The Theory behind Java Programming Language Part2.

IRAKOZE Yves
4 min readJul 13, 2020

How to write First Program in Java.

class firstProgram{

public static void main(String args[]){

System.out.println(“Hello Programmer”);

}

}

save this file as firstProgram.java

To compile: javac firstProgram.java

To execute: java firstProgram

it will give the output as Hello Programmer

Let see what this is :

class keyword is used to declare a class in java.

public keyword is an access modifier which represents visibility, it means it is visible to all.

static is a keyword if we declare any method as static, it is known as a static method. The main method is executed by the JVM(Java Virtual Machine), it doesn’t require to create an object to invoke the main method. so it saves memory.

void the return type of method, it means it doesn’t return any value.

main is an entry point of the program. The execution of programs starts from main. It is called by Runtime System.

String[] args is used for the command-line argument.

System.out.println() is used print statement.

Variables

A variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable’s memory; the range of values that can be stored with that memory; and the set of operations that can be applied to the variable.

You must declare all variables before they can be used. The basic form of a variable declaration shown here: data_type variable = value;

Here data type is one of Java’s datatypes and variables. To declare more than one variable of the specified type, you can use a comma-separated list.

Following are valid examples of variable and initialization in Java: int a, b, c;

//Declare three ints, a,b, and c.

Example of initialization

int a = 1, b = 2;

Constant: During the execution of a program, the value of a variable may change. A constant represents permanent data that never changes.

if you want to use some value likes p =3.14159; no need to type every time instead you can simply define constant for p, the following is the syntax for declaring constant.

Static final datatype ConstantName = value;

Example: static final float PI=3.14159;

Data type

Every variable in Java has a data type. Data types specify the size and type of value that can be stored.

Data types in Java divided types:

Primitive(intrinsic) data type and Non-primitive data type

Primitive data types contain Integer, Floating points, Characters, Booleans

Non-primitive types contain Classes, Interfaces, and Arrays.

Integer: This is a group that includes byte, short, int, and long, which are whole signed numbers.

Floating-point numbers: This group includes float and double, which represent the number with a fraction precision.

Characters: This group includes char, which represent character set like letter and number

Boolean: This group includes Boolean, which is special type of representation of true or false value.

Some data types with their range and size:

byte: -128 to +127 (1 byte)

short: -32,768 to +32,767 (2 byte)

int: -2,147,483,648 to +2,147,483,647(4 byte)

float: 3.4e-038 to 1.7e+038(4 byte)

double: 3.4e-038 to 1.7e+308(8 bytes)

boolean: can take only true or false (1 byte)

char: holds only a single character (2 bytes)

Variable scope

There are three kinds of variables in Java:

Local variable:

  • A variable that is declared inside the method is called a local variable
  • Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor or block
  • Access modifiers can not be used for local variable
  • Local variables are visible only within the declared method, constructor, or block.
  • there is no default value for local variables should be declared and an initial value should be assigned before first use.

Instance variable

  • A variable that is declared inside the class but outside the method is called an instance variable. It is not declared as static
  • an instance variable is created when an object is created with the use of keyword ‘new’ and destroyed when the object is destroyed
  • The instance variable holds values that must be referenced by more than one method, constructor, or block essential parts of an object’s state that must be present throughout the class.
  • Instance variable can be declared in a class level before or after use.
  • Access modifiers can be given for instance variables
  • instance variables have default values. for numbers, the default value is 0, for booleans, it is false and for object references it is null. Values can be assigned during the declaration or within the constructor.
  • The instance variable can be accessed directly by calling the variable name inside the class. However, within the static methods and different classes (when instance variables are given accessibility) should be called using the fully qualified name. ObjectReference. VariableName.

Class/static variable

  • A variable that is declared as static is called a static variable. it cannot be local.
  • Class variables also are known as a static keyword in a class, but outside a method, constructor, or block.
  • There would any be one copy of each class variable per class, regardless of how many objects created from it.
  • a static variable is stored in static memory
  • A static variable is created when the program starts and destroyed when the program stops
  • Visibility is similar to instance variables
  • A static variable can be accessed by calling with the class name ClassName.VariableName.

Example

class Simple {

int data = 12; // instance variable

static int m =10;// static variable

void method(){

int i=23;//local variable

}

}// end of call Simple

--

--