When should i use the { } braces in java pls can anyone answer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

When should i use the { } braces in java pls can anyone answer

When should i use the { } braces pls can anyone answer cuz evrrytime theres a error for it

19th Jun 2019, 11:00 AM
Samsung S3
Samsung S3 - avatar
5 Answers
+ 4
Curly braces denote a block of code with local scope { int n = 5; } System.out.println(n); This will lead to an error (unless there's a global variable n) because the scope of the variable n is only within the braces and the variable is unknown outside of the braces. If an if statement evaluates to true, the following line will be executed. This can be extended to a block of several lines of code by using {}. Same with loops etc.
19th Jun 2019, 12:14 PM
Anna
Anna - avatar
+ 4
someMethod(){Inside the braces is the body of this method when ever someMethod() is called what ever is inside here will be exectued}
19th Jun 2019, 10:47 PM
D_Stark
D_Stark - avatar
+ 2
In java: * classes public class ClassName{ } * methods public void print(){ } * constructors public ClassName{ } * loops while(){ } for(){ } * if statements if(){ } * switch statements switch(){ } ...
19th Jun 2019, 10:19 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Every time use brackets for content inside a for, while loop or if statement
19th Jun 2019, 11:03 AM
Pulkit Kamboj
Pulkit Kamboj - avatar
+ 1
It depends on which programming language you are talking about. If you are talking about java then it is used to enclose a block of statements after a if or loop. Eg. if(a>3) { B=5; C=6; D=7; } Without the braces only the first line would execute if the condition is true and the rest will execute always i.e. even if condition is false.
19th Jun 2019, 11:14 AM
DEVANSH BANSAL
DEVANSH BANSAL - avatar