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

2-Data Types and Variables

Java is a strongly typed language where all variables have a defined type. It defines eight simple data types including integers, floating-point numbers, characters, and booleans. Literals are used to represent constant values of these types in code.

Uploaded by

aakarsh204
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

2-Data Types and Variables

Java is a strongly typed language where all variables have a defined type. It defines eight simple data types including integers, floating-point numbers, characters, and booleans. Literals are used to represent constant values of these types in code.

Uploaded by

aakarsh204
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Data types,

Variables
and
Array
Java Is a Strongly Typed Language
• First, every variable has a type, every expression has a type,
and every type is strictly defined.
• Second, all assignments, whether explicit or via parameter
passing in method calls, are checked for type compatibility.
• There are no automatic conversions of conflicting types as in
some languages.
• The Java compiler checks all expressions and parameters to
ensure that the types are compatible.
• Any type mismatches are errors

• For example, in C/C++ you can assign a floating-point value to


an integer. In Java, you cannot.
The Simple Types
• Simple types represents single value
• Java defines eight simple (or elemental) types of data:
• These can be put in four groups:
1. integers - byte, short, int, and long, which are for whole valued signed
numbers.
2. Floating-point numbers - float and double, which represent numbers with
fractional precision.
3. Characters -char, which represents symbols in a character set, like letters
and numbers.
4. Boolean- boolean, it is a special type for representing true/false values.

Note: in C and C++ allow the size of an integer to vary based upon execution
environment. However, Java is different. Because of Java’s portability requirement,
all data types have a strictly defined range. For example, an int is always 32 bits,
regardless of the particular platform.
1. Integers
• Java defines four integer types :
• All of these are signed, positive and negative values. Java does not support
unsigned.
• The width and ranges of these integer types vary widely, as shown in this table:
• Name Width Range
• byte 8 –128 to 127
• short 16 –32,768 to 32,767
• int 32 –2,147,483,648 to 2,147,483,647
• long 64 –9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
• For example:
– byte a, b;
– Long p,q,r;
– Short x,y,z;
Example
• For example, here is a program that computes the number of miles that light will travel in a specified
number of days.
• // Compute distance light travels using long variables.
class Light {
public static void main(String args[]) {
int lightspeed;
int days;
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days);
System.out.print(" days light will travel about ");
System.out.println(distance + " miles.");
}
}
This program generates the following output:
In 1000 days light will travel about 16070400000000 miles.
2. Floating-Point Types
• Floating-point numbers, also known as real numbers, are used when evaluating expressions that require
fractional precision.

• There are two kinds of floating-point types, float and double, which represent single- and double-precision
numbers, respectively.
Name Width Approximate Range
1. double 64 4.9e–324 to 1.8e+308
2. float 32 1.4e−045 to 3.4e+038
• Here is a short program that uses double variables to compute the area of a circle: Compute the area of a circle.

class Area {
public static void main(String args[]) {
double pi, r, a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area

System.out.println("Area of circle is " + a);


}
}
3.Character
• Used to store characters
• char in Java is not the same as char in C or C++. In C/C++, char is an integer type that is 8 bits wide. in
Java char is a 16-bit type.
• The range of a char is 0 to 65,536.
• There are no negative chars.
• Here is a program that demonstrates char variables:
// Demonstrate char data type.
class CharDemo {
public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
• This program displays the following output:
– ch1 and ch2: XY
Another example
• Even though chars are not integers, in many cases you can operate on them as if
they were integers. This allows you to add two characters together, or to
increment the value of a character variable.

• // char variables behave like integers.


class CharDemo2 {
public static void main(String args[]) {
char ch1;
ch1 = 'X';
System.out.println("ch1 contains " + ch1);
ch1++; // increment ch1
System.out.println("ch1 is now " + ch1);
}
}
The output generated by this program is shown here:
ch1 contains X
ch1 is now Y
4. Boolean Type
• for logical values.
• It can have only one of two possible values, true or false.

• // Demonstrate boolean values.


class BoolTest {
public static void main(String args[]) {
boolean b;
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
// a boolean value can control the if statement
if(b)
System.out.println("This is executed.");

b = false;
if(b)
System.out.println("This is not executed.");
// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
Continue…
• The output generated by this program is
shown here:
b is false
b is true
This is executed.
10 > 9 is true
Literals.
• A constant value in Java is created by using a literal
representation of it. For example,
• here are some literals:
100 98.6 ‘X’ “This is a test”
(1) (2) (3) (4)
1. specifies an integer,
2. specifies a floating-point value.
3. specifies a character constant,
4. specifies a string.
Integer Literals.
• Any whole number value is an integer literal.
Examples are 1, 2, 3, and 42.
• Octal values are denoted in Java by a leading
zero as 07,06 but 09 is not.
• a hexadecimal constant with a leading zero-x,
(0x or 0X). The range of a hexadecimal digit is
0 to 15, so A through F (or a through f ) are
substituted for 10 through 15.
Floating-Point Literals
• They can be expressed in either standard or scientific notation.
• Standard notation consists of a whole number component followed
by a decimal point followed by a fractional component. For example,
2.0, 3.14159, and 0.6667 represent valid standard-notation
• Scientific notation consists of a floating-point number plus a suffix
that specifies a power of 10 by which the number is to be
multiplied.

Examples include 6.022E23, 314159E–05

• Floating-point literals in Java default to double precision. To specify


a float literal, you must append an F or f to the constant.
Boolean Literals
• There are only two logical values that a
boolean value can have, true and false.
• The values of true and false do not convert into
any numerical representation.
• The true literal in Java does not equal 1, nor
does the false literal equal 0.
• In Java, they can only be assigned to variables
declared as boolean, or used in expressions
with Boolean operators.
Character Literals
• Characters in Java are indices into the Unicode
character set.
• A literal character is represented inside a pair
of single quotes.
String Literals
• String literals in Java are specified by enclosing a
sequence of characters between a pair of double
quotes.
“Hello World”
“two\nlines”
“\”This is in quotes\””
• The escape sequences and octal/hexadecimal
notations that were defined for character literals
work the same way inside of string literals.

You might also like