-2
class AddMain {
    int a, b;

    public static void main(String ar[]){
        System.out.println(a + b);
    }
}

This is simple java program and my question is why we don't use semicolon at the end of the class braces. Please let me know and thanks to all.

mahesh singh
  • 202
  • 1
  • 12
  • 6
    Because that's the language. – Maroun Jun 11 '15 at 06:52
  • 5
    Because the syntax has been defined as such. – Kayaman Jun 11 '15 at 06:52
  • socket is because they are also a java programmer and i want to know the reason of that semi colon – mahesh singh Jun 11 '15 at 06:55
  • one more question is why java.lang package is auto imported in java – mahesh singh Jun 11 '15 at 06:55
  • Possible dupe: http://stackoverflow.com/questions/2724371/when-would-you-put-a-semicolon-after-a-method-closing-brace – Drakes Jun 11 '15 at 06:57
  • This is not so bad question and because there are languages without semicolon, it makes sense to ask why we need it in Java. – Michal Krasny Jun 11 '15 at 06:57
  • @vefthym huh? We don't need semicolon in Java? – Michal Krasny Jun 11 '15 at 07:00
  • It is the java designers' decision of syntax. – Naman Gala Jun 11 '15 at 07:00
  • 1
    Please try to be nice when commenting. I don't see anything wrong with this question. As a brief answer, C++ has a semicolon since you can declare an *instance* of that class at the end of the declaration. That makes little sense in Java where you can only have *references* to classes. So a semicolon would be superfluous. – Bathsheba Jun 11 '15 at 07:01
  • can you explain it more ? – mahesh singh Jun 11 '15 at 07:03
  • thanks in advance please let me know about it @Bathsheba – mahesh singh Jun 11 '15 at 07:03
  • Not until the question is reopened. If you can, try to improve it. Questions consisting of a title and code tend to rub folk up the wrong way. – Bathsheba Jun 11 '15 at 07:04
  • Why don't you use `;` at the end of `if(){}` or `for(){}` or `while(){}` ??? Its for the same reason... When you have a block of statements inside `{}`, a `;` is not required... – Codebender Jun 11 '15 at 07:05
  • i don't know the reason so i am asking about this please ans me and please let me know why java.lang package is automatically imported thanks – mahesh singh Jun 11 '15 at 07:07
  • @user2894408 I suggest that you do the following: 1. Edit the question, so that it can be re-opened. See instructions here: http://stackoverflow.com/help/how-to-ask Bathsheba or anyone else cannot add a new answer, until you do that 2. Ask a new question, or search other posts for why java.lang is automatically imported. You should not have two different (irrelevant) questions in the same post. – vefthym Jun 11 '15 at 07:10

1 Answers1

2

Semicolon in Java is a separator - it separates statements. But if you put some statements into a block of code using { ... a block of code ...}, then this separator is not needed, because the {} brackets do the separation themselves.

Have a look at an article like this one http://beginwithjava.blogspot.cz/2008/06/those-pesky-semicolons.html

Michal Krasny
  • 5,434
  • 7
  • 36
  • 64