Data Types, Variables and Constants
Unit Structure
2.1 Datatypes2.1.1 Integer data type
2.1.2 Floating point data type
2.1.3 Character data type
2.1.4 Boolean data type
2.2 Mixing Data types
2.3 Variables
2.3.1 Variable name
2.4 Constants
2.4.1 Integer Constant
2.4.2 Real Constant
2.4.3 Character Constant
2.4.4 String Constant
2.4.5 Symbolic constant
2.4.6 Backslash character constant
2.5 Comments
2.6 Command line arguments
2.7 Summary
2.1 Data types:
- A data type is a scheme for representing values. An example is int which is the Integer, a data type.
- Values are not just numbers, but any manner of data that a computer can process.
- The data type defines the kind of data that is represented by a variable.
- As with the keyword class, Java data types are case sensitive.
- primitive data type
- non-pimitive data type
- Numeric means Integer, Floating points
- Non-numeric means Character and Boolean
- classes
- arrays
- interface
Following table shows the datatypes with their size and ranges.
Fig: Datatypes with size and range
2.1.1 Integer data type:
Integer datatype can hold the numbers (the number can be positive number or negative number). In Java, there are four types of integer as follows:- byte
- short
- int
- long
We can make ineger long by adding ‗l‘ or ‗L‘ at the end of the number.
2.1.2 Floating point data type:
It is also called as Real number and when we require accuracy then we can use it.There are two types of floating point data type.
- float
- double
It is represent single and double precision numbers. The float type is used for single precision and it uses 4 bytes for storage space. It is very useful when we require accuracy with small degree of precision. But in double type, it is used for double precision and uses 8 bytes of starage space. It is useful for large degree of precision.
2.1.3 Character data type:
It is used to store single character in memory. It uses 2 bytes storage space.2.1.4 Boolean data type:
It is used when we want to test a particular condition during the excution of the program. There are only two values that a boolean type can hold: true and false.Boolean type is denoted by the keyword boolean and uses only one bit of storage.
Following program shows the use of datatypes.
Program:
import java.io.DataInputStream;
class cc2
{
public static void main(String args[]) throws Exception
{
DataInputStream s1=new DataInputStream(System.in);
byte rollno;
int marks1,marks2,marks3;
float avg;
System.out.println("Enter roll number:");
rollno=Byte.parseByte(s1.readLine());
System.out.println("Enter marks m1, m2,m3:");
marks1=Integer.parseInt(s1.readLine());
marks2=Integer.parseInt(s1.readLine());
marks3=Integer.parseInt(s1.readLine());
avg = (marks1+marks2+marks3)/3;
System.out.println("Roll number is="+rollno);
System.out.println("Average is="+avg);
}
}
Output:
C:\cc>java cc2
Enter roll number:
07
Enter marks m1, m2,m3:
66
77
88
Roll number is=7
Average is=77.0
2.2 Mixing Data types:
Java allows mixing of constants and variables of different types in an expression, but during assessment it hold to very strict rules of type conversion.When computer consider operand and operator and if operands are different types then type is automatically convert in higher type.
Following table shows the automatic type conversion.
2.3 Variables:
Variables are labels that express a particular position in memory and connectit with a data type.
The first way to declare a variable: This specifies its data type, and reserves memory for it. It assigns zero to primitive types and null to objects.
dataType variableName;
The second way to declare a variable: This specifies its data type, reserves memory for it, and puts an initial value into that memory. The initialvalue must be of the correct data type.
dataType variableName = initialValue;
The first way to declare two variables: all of the same data type, reserves memory for each.dataType variableNameOne, variableNameTwo;
The second way to declare two variables: both of the same data type, reservesmemory, and puts an initial value in each variable.
dataType variableNameI = initialValueI, variableNameII=initialValueII;
2.3.1 Variable name:
Use only the characters ‗a‘ through ‗z‘, ‗A‘ through ‗Z‘, ‗0‘ through ‗9‘, character ‗_‘, and character ‗$‘.- A name cannot include the space character.
- Do not begin with a digit.
- A name can be of any realistic length.
- Upper and lower case count as different characters.
- A name cannot be a reserved word (keyword).
- A name must not previously be in utilized in this block of the program.
2.4 Constant:
Constant means fixed value which is not change at the time of execution of program. In Java, there are two types of constant as follows:-Numeric Constants
Integer constant
Real constant
-Character Constants
Character constant
String constant
2.4.1 Integer Constant:
An Integer constant refers to a series of digits. There are three types of integer as follows:a) Decimal integer
Embedded spaces, commas and characters are not alloed in between digits.
For example:
23 411
7,00,000
17.33
b) Octal integer
It allows us any sequence of numbers or digits from 0 to 7 with leading 0 and it is called as Octal integer.
For example:
011
00
0425
c) Hexadecimal integer
It allows the sequence which is preceded by 0X or 0x and it also allows alphabets from ‗A‘ to ‗F‘ or ‗a‘ to ‗f‘ (‗A‘ to ‗F‘ stands for the numbers ‗10‘ to ‗15‘) it is called as Hexadecimal integer.
For example:
0x7
00X
0A2B
2.4.2 Real Constant
It allows us fractional data and it is also called as folating point constant.It is used for percentage, height and so on.
For example:
0.0234
0.777
-1.23
2.4.3 Character Constant
It allows us single character within pair of single coute.For example:
‗A‘
‗7‘
‗\‘
2.4.4 String Constant
It allows us the series of characters within pair of double coute.For example:
―WELCOME‖
―END OF PROGRAM‖
―BYE …BYE‖
―A‖
2.4.5 Symbolic constant:
In Java program, there are many things which is requires repeatedly and if we want to make changes then we have to make these changes in whole program where this variable is used. For this purpose, Java provides ‗final‘ keyword to declare the value of variable as follows:Syntax:
final type Symbolic_name=value;
For example:
If I want to declare the value of ‗PI‘ then:
final float PI=3.1459
the condition is, Symbolic_name will be in capital letter( it shows the difference between normal variable and symblic name) and do not declare in method.
2.4.6 Backslash character constant:
Java support some special character constant which are given in following table.Constant---Importance
'\b'---Back space
'\t'---Tab
'\n'---New line
'\\'---Backslash
'\''---Single coute
'\"'---Double coute
2.5 Comments:
A comment is a note written to a human reader of a program. The programcompiles and runs exactly the same with or without comments. Comments start
with the two characters ―//‖ (slash slash). Those characters and everything that follows them on the same line are ignored by the java compiler.
everything between the two characters ―/*‖and the two characters ―*/‖ are unobserved by the compiler. There can be many lines of comments between the ―/*‖ and the ―*/‖.
2.6 Command line arguments:
Command line arguments are parameters that are supplied to the application program at the time of invoking its execution. They must be supplied at the time of its execution following the file name.In the main () method, the args is confirmed as an array of string known as string objects. Any argument provided in the command line at the time of program execution, are accepted to the array args as its elements. Using index or subscripted entry can access the individual elements of an array. The number of element in the array args can be getting with the length parameter.
For example:
class Add
{
public static void main(String args[])
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c=a+b;
System.out.println(―Addition is=‖+c);
}
}
output:
c:\javac Add.java
c:\java Add 5 2
7
Learn and practice some important MCQ with answers on various categories useful for competitive exams
ReplyDeletehttp://www.gkindiaonline.com/