Variables in Java
Used to store data Variables in Java is a container which holds the value while the Java program is executed. When we use variables we can also change the data in later stages of program as Variable is a name of memory location rather than the data itself. As the name Variable itself suggests "vary + able" its value can be changed.
Let's understand it with the help of an example :
Here the dog in the middle represents the value while " white dog " , " black dog " and " golden dog " represents the variables.
Java Variable
Java Variable example of adding two numbers and printing the output.
public class Simple{ public static void main(String[] args){ int a=50; int b=20; int c=a+b; System.out.println(c); } }
Output
70
There are mainly three types of variables used in Java :
- local variable
- instance variable
- static variable
However there are two types of data types in Java:
- primitive data types
- non-primitive data types
Primitive data types in Java
Primitive data-types are the simplest/are less abstract of data used in java because they are already predefined, meaning that the values that they hold are already defined that is why you can store a string in an Int variable and are used to point very specific things like numbers, letters, alphanumeric values, booleans, decimal numbers, and other values.
There are eight primitive data types in Java :
boolean : the type whose values are either true or falsechar : the character type whose values are 16-bit Unicode characters
- Arithmetic Types:
- integral types:
- byte
- short
- int
- long
- the floating-point types:
- float
- double
The values of class type are references where as strings are references to an instance of class String.
Non-Primitive data types in Java
Non-Primitive data-types a.k.a the Reference type are are not pre-defined i.e the person who is programming using Non-Primitive data types has the liberty to customize and define these data-types (except for “String”).
Reference data types as the name suggests don't store the value, but store a reference to that value. Java keeps the reference, also called address, to that value, not the value itself where reference types can be a class, interface, or array variable.
Points to remember
- Non-primitive data-types are not pre-defined, the person who is programming has the liberty to customize and define these data-types(except for “String”).
- Non-primitive data types can have customized methods on them
- The size in memory of a primitive data-type is already defined but the size of a non-primitive depends on the programmer.
There are 4 main types of non-primitive data types :
- String
- Class
- Array
- Interface.
String
Strings are a group of characters surrounded by double-quotes. Strings are mostly used to store chunks of text and information. This class stores each character inside of an array.
so if we make a string that holds a name say “Technotoken”
String name = "Technotoken";
so what this string will really look like is {‘T’, ‘e’, ‘c’, ‘h’, ‘n’, ‘o’, ‘t’, ‘o’, ‘k’, 'e', 'n'}, this makes it possible to access each character individually like an array.
Class
Java being an object-oriented programming language due to which everything in Java is associated with classes and objects, along with its attributes and methods.
For example: in real life, a Pen is an object. That pen has many attributes, such as weight , name and color, and methods, such as writing and drawing. A Class is the blueprint from which every individual object is created.
//Pen Class in Java
public class Pen { public static void main(String args[]) { String name = "Parker"; String colour = "Golden"; int z=900; System.out.println("Name of the pen is : " + name); System.out.println("Colour of the pen is : " + colour); System.out.println("Price of the pen is : " + z); } }
Output
Name of the pen is : Parker
Colour of the pen is : Golden
Price of the pen is : 900
Array
Arrays are used for storing information in a “list” format. Arrays are very helpful for storing data that will not have a lot of added elements, for lists that require more manipulation than there are in Linked Lists. It is a collection of similar types of data.
For example, if we want to store the names of 500 people instead of say 1-2 people then we can create an array of the string type that can store 500 names.
String[] array = new String[
500];
Interface
An interface is a reference type, similar as class, that can contain only constants, method declaration(method signature, no body), default methods, static methods and nested types inside it's body. Nested type simply means it can contain another interface or class inside it.
An interface in java is declared using interface keyword, followed by interface name. After interface name it's interface body enclosed in { }.
The basic syntax of declaring an interface is :
interface interfaceName { // constant declarations // Method signatures }
Example :
// java interface Program interface MyInterface { // constant and Method declarations int id = 20; void print(); public int calculateArea(); }
No comments
If you have any doubts, please let us Know.