File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Coding Tricks

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the above code complied fine.
are there anymore coding tricks that may come in the scjp exam
 
Sven Mathijssen
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this.

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sven Mathijssen
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the compiler in that case will be because the int x variable will be locla variable and that should be initialized before using it
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

 
Piotr Nowicki
Ranch Hand
Posts: 610
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Divyeshh Patel
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic