Introduction to Java Programming

Introduction to Java Programming

exploreJava logo


Java is an Object-oriented programming language. It is compiled and interpreted language. Java program is first compiled with java compiler javac. The javac complier generates class file (Byte codes) if there is not errors in the program. That class file is interpreted with java interpreter. Java programs are portable. The complied java programs executes in any platform with Java Virtual Machine (JVM) installed. Java Virtual Machine is an operating environment where java programs are executed. JVM is available for all operating systems. To write a Java program class must be created. Class is created with keyword class. Java programs are case sensitive. It is necessary for the name, case of created class and name, case of file to match. Java is a typed language. Data types of variables must be declared. A variable defined of type "A" can't contain any other type of data values except of type "A". The primitive data types found in java are: int, float, char, short, byte, double, boolean, long. Java has a huge collection of in-built objects like String, HashMap, ArrayList, Stack, TreeMap, File, Connection and many more. A single java program can have more than one class defined. Java programs have main() method defined inside one of the classes, main() method takes array of strings as parameter. The execution of program starts from the main() method. Java programs are compiled and interpreted from the command line using javac compiler and java interpreter. If hello.java is a file name of the java program saved in java folder then in the command prompt following commands are typed to compile and interpret or execute the program:
  • java\>javac hello.java --> compiling with javac compiler. It creates a file named hello.class that contains the bytecode version of the java program. Java Byte code the intermediate representation of the program containing instructions for the JVM (Java Virtual Machine). JVM executes those instructions. To run java program Java application launcher known as java is required. All the individual class is put into its own output file named after the class with the .class extension.
  • java\>java hello --> interpreting (executing) with java interpreter.
  • Name of the java program is the name of the class in which main() method is defined.
  • 	class Demo{
    	public static void main(String[] param){
    		System.out.println("This text will be displayed");
    	}
    }
    	

    Here, in the given program, name of class containing main() method is hello, so the name of file is hello.java. The case of class name and file name must match. The execution of the Java programs start from the main function(), main function takes parameter; main() method/function is defined as public and static. The public keyword is an access specifier; access specifier controls the visibility of class members: variables and methods; public members are accessible outside of the class in which it is declared. The main function is declared public as it must be called by code outside of its class at the time of program starting. The keyword static allows main() to be called without need to creat object of the class in which main() is defined. The keyword void is written as main() does not return any value.

    Any information needed to pass to method is passed via parameters which are specified within a set of parenthesis of the method. In the main(), only one parameter is passed, and this is array of Strings.

    System.out.println() method prints string passed to the println() function. In the above program, This text will be displayed in command line, is displayed when executed. All statements in Java end with a semicolon. Lines in the program not ending with a semicolon are not technically statements in Java.

    Data Types: Data types specify type of values that to store. To store numbers int, long, byte, short are used; to store real numbers float or double is used, to store character, char data type is used. For integer variables, range of values are specified, each type of integer has range of values it can store. All integers in Java are signed, positive and negative values; unsigned, positive only integers are not supported by Java.

    NameWidth in BitsDescriptionRange
    double64double specifies double precision value
    float32single precision value
    char16Java uses Unicode to represent characters
    booleanUsed for logical values, it has only two values: true or false.
    int32Any whole number value is an integer literal.-2,147,483,648 to 2,147,483,647
    long64Any whole number value is an integer literal. Used to store big numbers-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
    short16Any whole number value is an integer literal.-32,768 to 32,767
    byte32Used to store small integers.-128 to 127

    Declaring a Variable

    	type indentifier [=value][,indentifier[=value]...];
    	int a, b, c; 
    	int x = 44, y = 45; 
    	double pi = 3.14159;
    	char a = 'x';
    	
    	class hypotenus{
    		public static void main(String[] param){
    			double b = 3.0, p = 5.0;
    			double hypo = Math.sqrt(b*b + p*p);
    			System.out.println("Hypotenuse is: "+hypo);
    		}
    	}
    	

    Here in the class, hypotenus, b and p are initialized with constants but hypo is initialized dynamically. This program uses Java's built-in method, sqrt(), member of Math class, to compute the square root of the argument (parameter) passed to it.

    Scope and Lifetime of Variables

    Variables can be declared within any block. A block begins with an opening curly brace and ends with closing curly brace. A block defines a scope. A scope determines what objects or variables are visible to other parts of your program and also determines the lifetime of those objects or variables.

    In Java, two major scopes are defined; one by a class and another by a method. Generally, variables declared in one scope are not visible to code that is defined outside that scope. Scopes can be nested.

    	class JavaDemo
    	{
    		public static void main(String[] oops){
    			int x;
    			x = 10;
    			{
    				int y = 35;
    				System.out.println("Sum of "+x+" and "+y+" is: "+(x+y));
    				x = x + 100;
    			}
    			//	y = 33; not possible to access y as it is in another scope
    			System.out.println("Value of x is: "+x); //displays 110
    		}
    	}
    	

    String Handling in Java

    Introduction to C Programming

    1 Comments

    Previous Post Next Post