Is there any technical reason behind it ??

Hello friends,
As we know that in c++ we end the class with a semicolon like this......
Class a
{
};
but in java we don't need to end the class defination with semicolon.
I want to know is there any technical reason behind it.

Replies

  • Leo
    Leo
    Surely there is a Technical reason. We know C++ is superset of C, C++ is developed by adding object oriented features of Simula to C, making it more powerful programming language than C itself, which gave C++ OOP feature of Simula by compromising little on speed with respect to C. We say C++ is Procedural Programing Language + OOP Language but the real thing is that C++ is still a Procedural C language with classes, in other words we can say that class definition is nothing but a statement written outside main function and every statement in C has to end with a semicolon.

    But when it comes to Java, Java is completely a OOP Language in which even main lies inside a class definition. Java derived its OOP feature from C++ itself but it never adopted any procedural feature which makes it a complete OOP language. Token parsing in Java is designed so as programmer can make program execution jump easily which clears that class is never held as a statement but rather as a package, whereas when it comes to C++ parsing is modeled as program should become more modular leading to bottom up approach in program execution, in other words a package in C++ never becomes a package unless and until we manually specify it to become a package. A package in C++ is nothing but a header file so please don't take it otherwise. Please feel free to ask if you haven't understood any point.Happy Computing.
  • Morningdot Hablu
    Morningdot Hablu
    You mean to say that it is because of main function declaration under class.

    Can you tell me what is Token parsing in Java??
  • sushant005
    sushant005
    class A
    {

    public void add()
    {
    int b=2;
    System.out.println("A=" +b);
    };
    public static void main(String[] a)
    {
    A p=new A();
    p.add();
    }
    }
    In the above Java program the class is ended with a semi-colon and works fine as C++.
  • Leo
    Leo
    mohit007kumar00
    You mean to say that it is because of main function declaration under class.

    Can you tell me what is Token parsing in Java??
    Hi Mohit tokens are nothing but numbers, characters, strings, floats, constants, variables and identifiers in any program. Parsing is nothing but the method of processing tokens when they are placed in Symbol Table. When program is compiled it is not compiled line by line, it is compiled statement by statement, when a statement is scanned, all tokens are separated and placed into a table for further processing along with their respective symbols and signs, this table is known as Symbol Table. The further processing of symbol table elements not only decides type of approach in programming language but also speed of execution. For C language it is top down approach which is fastest procedural parsing and hence no programming language is as fast as C language. In C++ for adding OOP feature to C++ parsing changed to bottom up but procedural parsing with respect to C was kept unchanged so C++ is second fastest programming language. In fact C and C++ are only programming language in this world which are challengers to assembly level programming. Where as in Java, parsing is so modeled that each class becomes a package so we can't really say program is using bottom up or top down approach hence we can say Java creators have used much complex parsing algorithms so as when a class becomes a complete package original program doesn't become an overload to memory and JVM.

    The fact is i don't really know what kind of parsing Java uses but it is one of the most advanced and complex type of parsing because nearly 80% web application run on Java and we can even build system and application software, even they run without any problem. I hope you got what token parsing is.
  • anandkumarjha
    anandkumarjha
    hello leo,
    can you help with my doubt that how actually java programs are being compiled and run to give output?
    and what is the use of static in (public static void main(string args[]).
  • sushant005
    sushant005
    As we all know that Java is compiled plus interpreted language.

    class A
    {
    public static void main(String [] a)
    {
    System.out.println("My first java program");
    }
    }
    Suppose the above program is your source code and it is compiled to give the Bytecode i.e .class file
    and in the second step/stage the Bytrecode is being interpreted by the JVM to give the output.
    In the JVM there is a Bytecode verifier which verifies the byutecode and checks the validity of classes ,variables and methods used in the program.
    Later on with the java version 1.2 JUST-IN -TIME compiler is released which is also a part of JVM.

    What is the use of static in public static void main(String[] arg)

    class A
    {
    public void main(String[] a)
    {
    System.out.println("Hello World");
    }
    }
    Consider the above program here static is not defined and this program compiled successfully but shows Exception in the thread main when it is interpreted by the JVM.
    In the above program if we create an object then also it shows an Exception.

    But with the static in the main()
    if we run the above program it runs successfully even without an object.
    So we can say that without creating the object of class JVM can directly execute the code.

    If any one having any other reason please add here!!A
  • anandkumarjha
    anandkumarjha
    thank you sushant for sharing your information with us
  • anandkumarjha
    anandkumarjha
    hello all,
    can you tell what exactly is the bitwise operator for in c language and how does it works?
  • Leo
    Leo
    anandkumarjha
    hello leo,
    can you help with my doubt that how actually java programs are being compiled and run to give output?
    and what is the use of static in (public static void main(string args[]).
    Hi Anand. Java is one of its kind programing language in which programs are not only compiled but also interpreted.
    Compilation is process in which code is completely checked for syntax and semantics.
    Interpretation is process in which program runs line by line after getting being checked.

    Both compiler and interpreter are subject to change with CPU architecture due to which we have separate compilers and interpreters for different processing technologies.

    When you compile any Java program it does not create any executable code rather creates a Java bytecode which is optimized version of original source code. The bytecode is stored in .class file which gets interpreted by JVM which is available for all platforms and hence Java always produce only portable codes that runs same on all kind of platforms. The strong exception handling in Java is possible just because of JVM running the actual byte code. Now please note bytecode is never turned into executable code which leads to slower processing and speed penalty. For hardcore Java applications and games this is not really acceptable, so Sun Microsystems started porting Java JIT (Just In Time) compilers with its J2SE edition which enables conversion of Java bytecode into executable code which leads to faster execution for games and hardcore Java applications such as Office suites. All Java bytecode do not convert into executable code at same time which leads to more loading time but application runs fine after conversion into executable code so when you close that program all executable binaries get deleted to protect your space and secure you from malicious execution of binaries, so when you start that program again it takes same time for loading and starting. Since i never programed more than servlets in Java i don't know how to use JIT.

    Please note that most people think executable code means the code that runs when double clicked, which is not true. In fact an executable code depends on which type of compiler and OS created that code, in windows we always work on GUI interface and windows has its own extension .exe for executable code, all executable files are supposed to run via shell, so when you double click any .exe file in windows it calls Program "Windows Shell Commander" which is pre-associated with all executable binaries to run .exe files. That means the file with .py extension and .out extension are also executable codes the only difference is that they are not associated with double click and has to be manually run by shell.

    Your next question was what static is doing in (public static void main (string args[])). static keyword tells compiler that main method will be called without creating any instance of the class in which you defined main. You might be knowing no method from a class can be called without instance of that class(i.e object) but static allows main to execute without any object of class.

    The last thing people think class name must be same as file name just because Java is case sensitive, the real reason is not that. Java holds each class as different compilation unit which leads each class gets identified as identifier and we all know, no two identifiers can have same names. So from next time don't get confused.


    If you have any other problem, be free to ask.Happy Computing.
  • Leo
    Leo
    sushant005
    In the above Java program the class is ended with a semi-colon and works fine as C++.
    Hi Shushant please have a careful look not class its the method of class is followed by a semicolon. Actually method ended when you put terminating brace. The semicolon acts as statement there and just a semicolon is nothing but a null statement, so there is actually nothing about terminating a class.
  • Leo
    Leo
    anandkumarjha
    hello all,
    can you tell what exactly is the bitwise operator for in c language and how does it works?
    Bitwise operators are nothing but bit manipulators used in logical operations, they work same as any logical operation.
    Following are the bitwise operators in C.

    ~ One's Compliment
    >> Right Shift
    << Left Shift
    & Bitwise And operator
    | Bitwise OR operator
    ^ Bitwise XOR operator
  • sushant005
    sushant005
    Leo
    The last thing people think class name must be same as file name just because Java is case sensitive, the real reason is not that. Java holds each class as different compilation unit which leads each class gets identified as identifier and we all know, no two identifiers can have same names. So from next time don't get confused.
    You mentioned that file name must be same as class name but i think it may or may not have same name it depends upon the access specifier public.
    If are adding public to your class but then it is compulsory to give file name same as class name.But if are not mentioned public before the class then you can save with any name.
    import java.io.*;
    class c
    {
    public static void main(String []args)
    {
    System.out.println("Hello World");
    }
    }
    Consider the above program i haven't mentioned public and my class name is c but you can save with .java extension.But thing is to noticed that while compiling the program using comd. prompt you have to give the file name same as you have saved but while interpreting the program you have to give the class name not the filename .

    Code for compilation:javac Anyfilename.java
    code for interpretation:java c
    using command prompt.
    The aabove code is for above program here my class name is c.

    do me correct if i am wrong!!
  • Leo
    Leo
    Hi Sushant i think you haven't read my previous reply. When you compile program it checks only for syntax and semantics, so source code compiles successfully and creates a bytecode just because there is nothing wrong in syntax and semantics. It runs successfully because every class itself acts as a package, hence even you define all classes in same source file, when you will compile it, the source will create multiple .class files. If you want to check try Main instead of main and even that will compile without error but will not run in interpreter because it will find main in bytecode not Main.

    Original execution takes place after interpretation and have a careful look after compilation the bytecode file is still a c.class file and not a anyfile.class, and c.class file is identifier for JVM and not anyfile.class. So this is just a basic example so was easy to find errors and execution. Try same thing in a real complex programing and you will find that when there comes any error it will be brain bombing to find where the real error may have happened. The example you mentioned is not exception and not even an error it just shows the same thing i told you before, each class itself is held as compilation unit not source code file though you can define as many classes in same .java file. I hope you understood. Feel free to ask whatever was cumbersome for you.Happy Computing.
  • sushant005
    sushant005
    Hi !! Leo i just want make clear that users have the facility that they can save their .java file with any name unless they are not using the public access specifier. If you want to check then write a java program using public access specifier and save you program with any name other than class name it surely shows an error that "you must save file name as class name which is public".This prove that file name must be same as class name.

    but, what if we write a program without using public access specifier and save the program with any file name will it show any error, no it will not show any error it simply compiles and gives the .class file and now we can interpret it easily and have the output without any exception.

    in short,
    public class {
    }
    here we are bound to save file same as class name.

    but,
    class {
    }

    here, we are not bound that we have save file name same as class name.We may or may not save as class name.
    And it is very clear that compiler is going to create .class file same as their class name only.
    Here i have given more focus on that we are not bound to save our program only as the class name.The thing is that whether user is using public before class are not.Once more if we are not using public then we may or may not save our file name same as class name.
  • Leo
    Leo
    Hi Sushant. Sorry might be my mistake i understood your question in wrong way. The first class you developed is [ 1.public class class_name<> ]. And the second was [ 2.class class_name<>]. I'll rather use type 1. & type 2. for above statements for explanation.

    Consider you created a program whose main program lies in class A. You define other two classes class C and D which will be used in Class A. The difference is C is defined as type 1 and D is defined as type 2. After compilation your program will execute successfully and you will be having three packages Package A, C and Package D.

    Now you create another program B in which you are going to use Class C and Class D. You import these classes as package and run your program, even this time no problem seems to be happening because there is nothing wrong anywhere. But import of packages were not as we supposed, C will be imported as independent package whereas D will be imported as sub-package with respect to Class A.

    Class D became dependent and hence as a sub-package of A it can always be identified without any problem, because it have reference form A, same thing is not applicable to Class C which is independent this means it will need himself a reference which is identifier and that identifier must be its own name. Even this example proves that class is compiled as separate unit and not as a complete program.

    I hope this is quite easy to understand. Happy Computing.
  • sushant005
    sushant005
    Leo
    Hi Shushant please have a careful look not class its the method of class is followed by a semicolon. Actually method ended when you put terminating brace. The semicolon acts as statement there and just a semicolon is nothing but a null statement, so there is actually nothing about terminating a class.
    Hi leo!!
    Good noticed...
    That Semi-colon was misplaced...no problem if you end class with a semi -colon in java programming.
    And what you have said i think is correct...
    class A
    {

    public void add()
    {
    int b=2;
    System.out.println("A=" +b);
    }
    public static void main(String[] a)
    {
    A p=new A();
    p.add();
    }
    } ;
  • HImanshu_Sharma
    HImanshu_Sharma
    hey leo you have a good knowledge of java yar..actually i want to reply all these question,,but after reading youre replies there is nothing so much to write here..keep it up...

You are reading an archived discussion.

Related Posts

Hello Fellow Mechanical and Civil Engineers, Being a mechanical engineer by trade, I was tired of my kids playing mindless video games for hours on end. I decided to build...
Is it possible to read sentences without using scanf in c programming ??

hi

i m newbie😎
Can any one suggest me a voice changer software for S60 series mobile ............... I hav heard about micromax mobile wich comes with a magic voice application, which converts male...
Can someone pleaaase tell me the importance of the value of r(frequency ratio)=sqrt(2) why does the value of displacement transmissibility becomes "1" for any given value of damping ( Mechanical...