Core Java - Java BasicsWhen we consider a Java program, it can be defined as a collection of objects that communicate via invoking each other's methods. Let us now briefly look into what do class, object, methods and instance variables mean. Now we are going to write first Java Program:Let us look at simple code that would print the word Hello World.public class SampleProgram{ public static void main(String[]args){ System.out.println("Hello World");// prints Hello World } }Let's look at how to save the file, compile and run the program. Please follow the steps given below: Output: C :> javac SampleProgram.java C :> java SampleProgram HelloWorld Basic Syntax:The following points are the rules of java created by java programmer Eg: class SampleProgram Eg: public void myMethodName() Eg: Assume 'SampleProgram' is class name, then the file should be saved as'SampleProgram.java' Java Identifiers:All Java components require names. In java, we have components such as class, methods, variables, interface and etc., for whom we can give the names to components are called as identifiers. In Java, there are several points to remember about identifiers where as:Java Modifiers:In other language, it is used to set the access level for classes, attributes, methods and constructors. We divide modifiers into two groups:Eg:public,private,default,protected. Eg:final,abstract,static etc., Java Variables:In java, A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type. Variable is a name of memory location. The types of variables in java are:Java Keywords:The following list shows the reserved words in Java. These reserved words may not be used as constant or variable or any other identifier names.class public private protected int byte short long char abstract boolean break final finally static default extends implements interface switch continue do float double if else interface for import new package super while return synchronized throws case catch enum this transient throw try void assert const goto instanceof native strictfp volatile Java Comments:Java supports single-line and multi-line comments very similar to c and c++. All characters available inside any comment are ignored by Java compiler.Eg: public class SampleProgram{ // This is an example of single line comment /* This program is of creating SampleProgram and printing the word Hello World. This is called as Multiline comment */ public static void main(String[]args){ System.out.println("Hello World"); } } ☛ Join to Learn from Experts: Java / J2EE Training in Chennai by TesDBAcademy
« Previous
Next Topic »
(Core Java - Objects and Class)
|