• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Coding Tricks

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the above code complied fine.
are there anymore coding tricks that may come in the scjp exam
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this.

 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic