0% found this document useful (0 votes)
13 views

6 Datatype

Uploaded by

SATYA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

6 Datatype

Uploaded by

SATYA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

A.B.N &P.R.

R COLLEGE OF
SCIENCE,KOVVUR

SUBJECT: OBJECT ORIENTED


PROGRAMMING THROUGH JAVA

CLASS :IIB.SC(M.P.CS,M.S.CS,M.C.CS)

LECTURE 14: DELIVERED BY M.MURTHY


Data types in java :-
 Data-type specifies type of the data.
 It specifies the size and what type of value a variable can store.
 Every variable has a data-type.
 Java language has rich set of data types.
Data types are categories into two types.
1. Primitive Data type.
2.Non-primitive types.
Primitive Data type :-Primitive types are predefined (already defined) in java. It
specifies size and value. The size of a primitive type depends on the data type.
Non –Primitive Data type :-Non-primitive types are created by the programmer
and is not defined by Java (except for String).Non-primitive types can be used to
call methods to perform certain operations, while primitive types cannot.
1. Integer type:

An integer type of variables can hold integer constants. Java supports four types of

integer data types. They are byte, short, int and long. The following table shows size

in bytes and range of values each data type can represent.


Integer Types:
2. Floating point type:
Floating type is used to represent numbers containing fractional values. They are
two types of data-types, float and double. Float type number are single precision
numbers while double type number are double precision numbers.

. type Size Maximum Value Minimum Value


float 4 bytes 3.4e-038 3.4e+038
double 8 bytes 1.7e-308 1.7e+308
Variables:
A variable is a name of a storage location and it is used to store a data value.
Unlike constants, a variable may take different values in different times during the
execution of the program.
A variable names may consist of alphabets, digits, the underscore(_) and dollar
characters, subject to the following conditions:
Rules for defining variable names :-
1. They must not begin with a digit.
2. Uppercase and lowercase are distinct. This means that the
variable Total is not the same as total or TOTAL.
3. It should not be a keyword.
4. White space is not allowed.
5. Variable names can be of any length.
Declaration of variables:
Variables used in program must be declare before using it in any statement.
The type declaration statement is used to declare the type of variables being used in
the program. The general form declaring a variable is
Syntax:- 1.Datatype variablename;
int x;//Here x is a variable of integer data type.
2.Data type variablename1,variablename2,….;
ex: int x,y,z;
float p,qr,r;
double d1,d2;
datatype variablename=value;(initializing)
Giving Values To Variables:A variable is given value after it is declared. This is
called giving values to variables. The general form of giving values to variables is
Example:- int x=10;//here we use assignment operator for giving value to a variable.
float f=1.467;
boolean b=true;
Valid Variable names:
$$_
R2D2
INT okay. “int” is reserved, but case is different here
_dogma_95_
riteOnThru
SchultzieVonWienerschnitzelIII

Invalid Variable names:


30DayAbs starts with a digit
2 starts with a digit
pork&beans `&‟ is illegal
private reserved name
C-3PO `-‟ is illegal
•Examples :-
1. int x, y; // x and y are integer variables
2. double d; // d is a double variable
3. String s; // s is a string variable
4. boolean b; // b is a boolean variable
5. char c; // c is a character variable
6. x = 7; // legal (assigns the value 7 to x)
7. b = true; // legal (assigns the value true to b)
8. c = „#‟; // legal (assigns character # to c)
9. s = “cat” + “bert”; // legal (assigns the value “catbert” to
s)
10. d = x – 3; // legal (assigns the integer value 7 –
3 = 4 to double d)
11. b = 5; // illegal! (cannot assign int to
boolean)
12. y = x + b; // illegal! (cannot add int and
boolean)
13. c = x; // illegal! (cannot assign int to char)
class Test1 {
public static void main(String args[])
{
byte a = 126;

// byte is 8 bit value


System.out.println(a);

a++;
System.out.println(a);

// It overflows here because


// byte can hold values from -128 to 127
a++;
System.out.println(a);

// Looping back within the range


Output:-
a++;
126
System.out.println(a);
127
}
-128
}
-127

You might also like