Core Java - SPLessons

Java Variables

Chapter 8

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Variables

Java Variables

shape Description

Java Variables are used to store the information about the programs. All the Java Variables are identifiers to memory location. Internally, all data are stored in memory location, each memory location is identified by using variables or the name given to the memory location address. In Java, two types of variables can be developed.

1. Variable Declaration

shape Syntax

In Java Variables, the declaration of variable will be as follows.

type variablename;

Where type is the type of information which is going to store (data type) and variable name can be represented with identifiers. For example, int num;

2. Variable Initialization

shape Syntax

Java Variables Initialization helps in storing or arranging the values in variables.

variable_name=value;

shape Examples


3. Variable Utilization

shape Description

Java Variables, the declared variables can be used in the program in various ways.For example,

System.out.println("Sum="+num);

+ is used for concatenation.
System.out.println is used to print variables value.

Java Datatypes

shape Description

Datatype is used to store the particular data, while declaring any variable developer has to specify datatype to store the variable. Datatypes can be defined as two types as follows.


Predefined Datatype

shape Description

There are 8 types of predefined datatypes.

shape Examples


User defined Datatype

shape Description

In java, there is no string datatype. To store string use string as user defined one.

Java Methods

shape Description

A method is a block of statements developed for reusable purpose. Method declaration syntax will be as follows.

modifier returntype methodName()
{
--------------------------------
--------------------------------
return value;
}

For more detailed overview on Click Here .
Return Type specifies the output type of a function. The output type can be a number,text etc..

Method specifications

shape Description

Method should be written inside class body and outside main method.


Return Type

shape Description

Return type indicates the type of the output. If the return type is void then the method shouldn’t return any value.Inside println statements we cannot invoke a method whose return type is void.

Arguments

shape Description

The arguments of the methods is used to read the inputs during invocation.The arguments are nothing but variable declaration.

Modifiers

shape Description

The scope of the variables defines from where it can be accessible. The scope of the variable is decided based on where the variable is declared. If we declare a variable within a body, such variables are called as local variables, the scope of the variable is limited to the body or context.

shape Example

package com.spl.methods;

public class Methods {

	//arguments are local to method body
	static int add(int arg1,int arg2)
	{
		System.out.println("Adding 2 numbers:");
		int res=arg1+arg2;
		System.out.println("num1="+arg1);
		System.out.println("num2="+arg2);
		//returning int type
		return res;

	}
	//main method should have access modifier "public"
	public static void main(String[] args) {
		//invoke a method and store result in a variable
		int retValue=add(12,10);
		//printing result
		System.out.println("result="+retValue);
	}

}

Output:

Adding 2 numbers:
num1=12
num2=10
result=22

Access Specifiers

shape Description

The members of the class can be provided access by using the access specifiers. The Access Modifiers defines the visibility level of the members. Java provides 4 access specifiers.

1)private:


The following is an example for the private access modifier.

    class A{  
    private int info=10;  
    private void msg(){System.out.println("Welcome to splessons");}  
    }  
      
    public class Simple{  
     public static void main(String args[]){  
       A obj=new A();  
       System.out.println(obj.info);//Compile Time Error   
       obj.msg();//Compile Time Error  
       }  
    }  

In the above example, two classes A and Simple are created. A class will have private data member as well as private method. Here user is accessing these private members from outside the class that’s why compile time error will be raised.

2) default:


The following is an example for the default.

    //save by A.java  
    package splesson1;  
    class A{  
      void msg(){System.out.println("Welcome to splessons");}  
    }  
    //save by B.java  
    package splesson2;  
    import splesson1.*;  
    class B{  
      public static void main(String args[]){  
       A obj = new A();//Compile Time Error  
       obj.msg();//Compile Time Error  
      }  
    }  

In the above example, class A and method msg() scope is default that is the reason it can’t be accessed from outside the package.

3) protected:


The following is an example for the protected.

    //save by A.java  
    package splesson1;  
    public class A{  
    protected void msg(){
System.out.println("Welcome to splessons");}  
    }  
    //save by B.java  
    package splesson2;  
    import splesson1.*;  
      
    class B extends A{  
      public static void main(String args[]){  
       B obj = new B();  
       obj.msg();  
      }  
    }  

Output:

Welcome to splessons

In the above example, The A class is public so it can be accessed from outside the package. Where as msg method is declared as protected that’s why it can be accessed from outside the class only by using inheritance.
4) public:


A class can refer another class present in different package by using fully qualified class name. Any class referred with package name and the class name is known as fully qualified class name. Import statement can also be used to import the members of a class from other package.

shape More Info

1)The private members has very low visibility and are highly secured.Public members has more visibility but low secured.Going from private to public, visibility increases,security decreases.

2)Developing a class inside another class is known as inner class.Java supports 4 types of inner class

3)For inner class all the 4 access specifiers can be used and inner class can be static or non-static.
4)Outer class cannot be static and for outer class, either public or default access are used.
5)In a java file, only one public class or interface can be developed. In such case, file name should be public class or interface.

Summary

shape Key Points

  • The scope of public specifier is global.
  • The String is the class and reference datatype in Java.
  • Separate memory will be allocated for the Java Variables.