dcsimg
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: Java
Expertise: Intermediate
Sep 18, 2018

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Using Braces Effectively in Java

Although using braces is a a common practice, it is sometimes very misleading and it can take hours to figure out what is wrong.

A simple way to avoid such problems is to use the curly braces ({}) as needed to avoid ambiguity.

Below is a simple code sample that does not have braces, and hence, there are problems during compilation.

class UsingBraces
{
   public static void main(String args[])
   {
      UsingBraces usingBraces = new UsingBraces();
      usingBraces.proceed();      
   }
   
   private void proceed()
   {
      for (int i=0;i           System.out.println("i: " + i); System.out.println("i: " + 5);
      //In the above line, the statement System.out.println("i: " + 5); is not part of the loop. 
      //If you try replacing the value 5 as i, the compiler itself throws an error indicating that the variable i cannot be identified.
   }
}

/*

Expected output:

[root@mypc]# java UsingBraces
i: 0
i: 1
i: 5

*/ 
M S Sridhar
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date