File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Coding Tricks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Coding Tricks" Watch "Coding Tricks" New topic
Author

Coding Tricks

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634



the above code complied fine.
are there anymore coding tricks that may come in the scjp exam


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Sven Mathijssen
Greenhorn

Joined: Aug 29, 2010
Posts: 19
mohitkumar gupta wrote:

the above code complied fine.
are there anymore coding tricks that may come in the scjp exam


Of course it compiles, it's just a variable declaration followed by an empty statement, which is perfectly legal.

By the way, the book is *drenched* in warnings about there being 'trick' questions and such on the exam.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Try this.



|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

mohitkumar gupta wrote:

the above code complied fine.
are there anymore coding tricks that may come in the scjp exam


but if you try to do this
System.out.println(x);
then it will throw runtime exception


SCJP6.0,My blog Ranchers from Delhi
Sven Mathijssen
Greenhorn

Joined: Aug 29, 2010
Posts: 19
Shanky Sohar wrote:
mohitkumar gupta wrote:

the above code complied fine.
are there anymore coding tricks that may come in the scjp exam


but if you try to do this
System.out.println(x);
then it will throw runtime exception


No it does not:



Result:


0


(Built in Eclipse, ran with Java 1.6.0_20).
Sven Mathijssen
Greenhorn

Joined: Aug 29, 2010
Posts: 19
Shanky Sohar wrote:
mohitkumar gupta wrote:

the above code complied fine.
are there anymore coding tricks that may come in the scjp exam


but if you try to do this
System.out.println(x);
then it will throw runtime exception


OK, and this:



Will generate a compile time error: x might not have been initialized.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

No it does not:



Result:


0


(Built in Eclipse, ran with Java 1.6.0_20).


this is correct for instance or class and static variable.but not for local varaiables
for example..this will not work



also by mistake i say runtime error above but it will be compile time error
Sven Mathijssen
Greenhorn

Joined: Aug 29, 2010
Posts: 19
Shanky Sohar wrote:
this is correct for instance or class and static variable.but not for local varaiables
for example..this will not work

also by mistake i say runtime error above but it will be compile time error


You are completely right, but the compile time error is not due to the extra semicolon at the end. If you omit the semicolon, it will still not compile.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Sven Mathijssen wrote:
You are completely right, but the compile time error is not due to the extra semicolon at the end. If you omit the semicolon, it will still not compile.


i know that..
it is because we are trying to use the variable before initializing it.

so be careful in such situation
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

the compiler in that case will be because the int x variable will be locla variable and that should be initialized before using it

SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Prasad Kharkar wrote:the compiler in that case will be because the int x variable will be locla variable and that should be initialized before using it


Keep it Up..Prasad Kharkar
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
One more I came across (don't exactly remember where, hence the code might not even be an exact copy of what I saw earlier, but this code is also perfectly valid and compiles fine)


Cheers,
Divyesh.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

One thing I bumped into recently is that


compiles and runs fine but shows warnings about 'dead code', while this


doesn't even compile because of unreachable code.


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37
Pedro Kowalski wrote:One thing I bumped into recently is that


compiles and runs fine but shows warnings about 'dead code', while this


doesn't even compile because of unreachable code.

This is because of the Java Language Specification. JLS allows if(false) to compile so that the debug statements can be turned on and off using static constants. e.g. if(obj.FALSE) s.o.p.("write something to log");
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

some about While statement from JLS

If the value is true, then the contained Statement is executed. Then there is a
choice:
◆ If execution of the Statement completes normally, then the entire while
statement is executed again, beginning by re-evaluating the Expression.
◆ If execution of the Statement completes abruptly, see §14.12.1 below.
• If the (possibly unboxed) value of the Expression is false, no further action
is taken and the while statement completes normally.
If the (possibly unboxed) value of the Expression is false the first time it is evaluated,
then the Statement is not executed.


That why it is showing unreachable code error
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Coding Tricks