#Topic-5
Previous topic (4) we have discussed about how to start coding! , we have gone through our first Java Program line by line. If you have not gone through that topic, click this link Your First Java Program: Hello World
In this section we will discuss about variable , Data types , Literals and many more!
Variable
Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a combination of "vary + able" that means its value can be changed.
Example:
int value=10;//Here value is variable
float a=10.50;//Here a is variable
There are three major types of variables in java:
- local variable
- instance variable
- static variable
A variable which is declared inside the method is called local variable.
2) Instance Variable :
2) Instance Variable :
A variable which is declared inside the class but outside the method, is called instance variable . It is not declared as static.
3) Static variable :
A variable that is declared as static is called static variable. It cannot be local.
We will have detailed learning of these variables in next chapters.
3) Static variable :
A variable that is declared as static is called static variable. It cannot be local.
We will have detailed learning of these variables in next chapters.
Example to understand the types of variables in java
class A{
class A{
int value=50;//instance variable
static int a=100;//static variable
void method() //method
{
int n=90;//local variable
} //end of method
} //end of class
static int a=100;//static variable
void method() //method
{
int n=90;//local variable
} //end of method
} //end of class
Data Types
Datatype is a special keyword used to allocate sufficient memory space for the data, in other words Data type is used for representing the data in main memory (RAM) of the computer.Data types represent the different values to be stored in the variable.
In java, there are two types of data types:- Primitive data types
- Non-primitive data types
Primitive data types
Primitive data types are those whose variables allows us to store only one value but they never allows us to store multiple values of same type.A variable takes up a certain amount of space in memory. How much memory a variable takes depends on its data type. A variable of a primitive data type contains the value of the variable directly in the memory allocated to the variable. For instance, a number or a character.
There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword.
Let us now look into the eight primitive data types in detail.
1) byte
- Byte data type is an 8-bit signed two's complement integer
- Minimum value is -128 (-2^7)
- Maximum value is 127 (inclusive)(2^7 -1)
- Default value is 0
- Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer.
- Example: byte a = 100, byte b = -50
2) short
- Short data type is a 16-bit signed two's complement integer
- Minimum value is -32,768 (-2^15)
- Maximum value is 32,767 (inclusive) (2^15 -1)
- Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an integer
- Default value is 0.
- Example: short s = 10000, short r = -20000
3) int
- Int data type is a 32-bit signed two's complement integer.
- Minimum value is - 2,147,483,648 (-2^31)
- Maximum value is 2,147,483,647(inclusive) (2^31 -1)
- Integer is generally used as the default data type for integral values unless there is a concern about memory.
- The default value is 0
- Example: int a = 100000, int b = -200000
4) long
- Long data type is a 64-bit signed two's complement integer
- Minimum value is -9,223,372,036,854,775,808(-2^63)
- Maximum value is 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
- This type is used when a wider range than int is needed
- Default value is 0L
- Example: long a = 100000L, long b = -200000L
5) float
- Float data type is a single-precision 32-bit IEEE 754 floating point
- Float is mainly used to save memory in large arrays of floating point numbers
- Default value is 0.0f
- Float data type is never used for precise values such as currency
- Example: float f1 = 234.5f
6) double
- double data type is a double-precision 64-bit IEEE 754 floating point
- This data type is generally used as the default data type for decimal values, generally the default choice
- Double data type should never be used for precise values such as currency
- Default value is 0.0d
- Example: double d1 = 123.4
7) boolean
- boolean data type represents one bit of information
- There are only two possible values: true and false
- This data type is used for simple flags that track true/false conditions
- Default value is false
- Example: boolean value = true
8) char
- char data type is a single 16-bit Unicode character
- Minimum value is '\u0000' (or 0)
- Maximum value is '\uffff' (or 65,535 inclusive)
- Char data type is used to store any character
- Example: char letterA = 'A'
Short Description of primitive data type :
Non-primitive data types
Non-primitive data types are not defined by the programming language, but are instead created by the programmer. They are sometimes called "reference variables," or "object references," since they reference a memory location, which stores the data. In the Java programming language, non-primitive data types are simply called "objects" because they are created, rather than predefined. While an object may contain any type of data, the information referenced by the object may still be stored as a primitive data type.
Non-primitive data types are also known as reference type, non-primitive data types are object , array, string, interface etc.
Non-primitive data types are also known as reference type, non-primitive data types are object , array, string, interface etc.
1. array type
// Points to an array instance
// Points to an array instance
Like the other non-primitive or reference data types, the array doesn't hold the actual value, but an address in memory. To show this in action, take a look at the following code. We'll fill some of the buckets with values, and print out the value of the array we created.
Example :int[] values= new int [10];
values[1]=10;
values[5]=20;
System.out.print(values); //it will print reference value like: [I@15db9742
2.class type
// Points to an object or a class instance
// Points to an object or a class instance
In order to create a new non-primitive or reference variable for this class, we have to create a new instance of the Vehicle class. The new keyword is used to create an object. Look at the following example where we'll be creating a new Vehicle called car.
The Java code is as follows:
Example :
Class Vehicle
{
//class specific code here
}
Vehicle car =new Vehicle(); // creating a new Vehicle called car.
System.out.print(car); //reference value will display
3.interface type
// Points to an object and a method, which is implemented to the corresponding interface
// Points to an object and a method, which is implemented to the corresponding interface
An interface is like a dashboard or control panel for a class. It has the buttons, but the function is elsewhere. We won't go into detail on implementing interfaces since the focus is on the interface as a non-primitive, or reference, data type.
Since we have the interface, we can create a variable, much like we did with the class.
4.String
//Here
s
is a reference variable, though it doesn’t point to any ObjectBy literal we mean any number, text, or other information that represents a value. This means what you type is what you get. We will use literals in addition to variables in Java statement. While writing a source code as a character sequence, we can specify any value as a literal such as an integer. This character sequence will specify the syntax based on the value's type. This will give a literal as a result. For instance
Integer Literals
Integer literals is a sequence of digits and a suffix as L. To represent the type as long integer we use L as a suffix. We can specify the integers either in decimal, hexadecimal or octal format. To indicate a decimal format put the left most digit as nonzero.
Character Literals
We can specify a character literal as a single printable character in a pair of single quote characters such as 'a', '#', and '3'. You must be knowing about the ASCII character set. The ASCII character set includes 128 characters including letters, numerals, punctuations etc. There are few character literals which are not readily printable through a keyboard.
Boolean Literals
The values true and false are also treated as literals in Java programming. When we assign a value to a boolean variable, we can only use these two values. Unlike C, we can't presume that the value of 1 is equivalent to true and 0 is equivalent to false in Java. We have to use the values true and false to represent a Boolean value. Like
boolean chosen = true;
Remember that the literal true is not represented by the quotation marks around it. The Java compiler will take it as a string of characters, if its in quotation marks.
Floating-point literals
Floating-point numbers are like real numbers in mathematics, for example, 4.13179, -0.000001. Java has two kinds of floating-point numbers: float and double. The default type when you write a floating-point literal is double.
A floating-point literal can be denoted as a decimal point, a fraction part, an exponent (represented by E or e) and as an integer. We also add a suffix to the floating point literal as D, d, F or f. The type of a floating-point literal defaults to double-precision floating-point.
The following floating-point literals represent double-precision floating-point and floating-point values.
String Literals
The string of characters is represented as String literals in Java. In Java a string is not a basic data type, rather it is an object. These strings are not stored in arrays as in C language. There are few methods provided in Java to combine strings, modify strings and to know whether to strings have the same value.
We represent string literals as
String myString = "How are you?";
The above example shows how to represent a string. It consists of a series of characters inside double quotation marks.
Null Literals
The final literal that we can use in Java programming is a Null literal. We specify the Null literal in the source code as 'null'. To reduce the number of references to an object, use null literal. The type of the null literal is always null. We typically assign null literals to object reference variables. For instance
s = null;
This example an object is referenced by s. We reduce the number of references to an object by assigning null to s. Now, as in this example the object is no longer referenced so it will be available for the garbage collection i.e. the compiler will destroy it and the free memory will be allocated to the other object. Well, we will later learn about garbage collection
Next week I will post about(#Topic-6) What is wrapper class in Java and its use?
If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook. Thank you! - Shivananda Rai
We can specify a character literal as a single printable character in a pair of single quote characters such as 'a', '#', and '3'. You must be knowing about the ASCII character set. The ASCII character set includes 128 characters including letters, numerals, punctuations etc. There are few character literals which are not readily printable through a keyboard.
The values true and false are also treated as literals in Java programming. When we assign a value to a boolean variable, we can only use these two values. Unlike C, we can't presume that the value of 1 is equivalent to true and 0 is equivalent to false in Java. We have to use the values true and false to represent a Boolean value. Like
boolean chosen = true;
Remember that the literal true is not represented by the quotation marks around it. The Java compiler will take it as a string of characters, if its in quotation marks.
Floating-point literals
Floating-point numbers are like real numbers in mathematics, for example, 4.13179, -0.000001. Java has two kinds of floating-point numbers: float and double. The default type when you write a floating-point literal is double.
A floating-point literal can be denoted as a decimal point, a fraction part, an exponent (represented by E or e) and as an integer. We also add a suffix to the floating point literal as D, d, F or f. The type of a floating-point literal defaults to double-precision floating-point.
The following floating-point literals represent double-precision floating-point and floating-point values.
String Literals
The string of characters is represented as String literals in Java. In Java a string is not a basic data type, rather it is an object. These strings are not stored in arrays as in C language. There are few methods provided in Java to combine strings, modify strings and to know whether to strings have the same value.
We represent string literals as
String myString = "How are you?";
The above example shows how to represent a string. It consists of a series of characters inside double quotation marks.
Null Literals
The final literal that we can use in Java programming is a Null literal. We specify the Null literal in the source code as 'null'. To reduce the number of references to an object, use null literal. The type of the null literal is always null. We typically assign null literals to object reference variables. For instance
s = null;
This example an object is referenced by s. We reduce the number of references to an object by assigning null to s. Now, as in this example the object is no longer referenced so it will be available for the garbage collection i.e. the compiler will destroy it and the free memory will be allocated to the other object. Well, we will later learn about garbage collection
Next week I will post about(#Topic-6) What is wrapper class in Java and its use?
If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook. Thank you! - Shivananda Rai
Comments
Post a Comment