0% found this document useful (0 votes)
36 views49 pages

3.1 Datatypes

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

3.1 Datatypes

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

Primitive Data types

in Java

By Upekha Vandebona
Java Is a Strongly Typed
Language.
Loosely typed languages: PHP,
JavaScript.
Every variable has a type, every
expression has a type, and every
type is strictly defined.
The Java compiler checks all
expressions and parameters to
ensure that the types are compatible.
Declaring a Variable
Java cares about type.

Primitive
Variable
Object
Reference
8 Primitive Types are

Integers, whole-valued
byte, short, int, long, signed numbers.

float, double, Floating-point numbers,


numbers with fractional precision.
char, and boolean
Characters, Boolean
symbols in a character set, special type for representing
like letters and numbers. true/false values.
Variables must have a
type.

Variables must have a


name
Primitive Type Ranges
 The primitive types are defined to have an
explicit range.
 Languages such as C and C++ allow the size
of an integer to vary based upon the
execution environment.
 Because of Java’s portability requirement,
all data types have a strictly defined range.
 For example, int is always 32 bits, regardless of
the particular platform.
Integer Types
Java defines four integer types:
byte, short, int, and long.
All of these are signed, positive
and negative values.
byte short int long
8 16 32 64 Four integer
primitives in Java.

small short tall grand


eIt holds something.
Primitive variables are like the cups they have at the coffee shop..
byte short int long
8 16 32 64 Four integer
primitives in Java.

I’d like an int please, with the value


of 2486, and name the variable
height.

small short tall grand


eIt holds something.
Primitive variables are like the cups they have at the coffee shop..
Declaring a Variable
Be sure the value can fit into the
variable.

But what about the other


way - pouring a small cup
into a big one?
No problem.
int
 The most commonly used integer type
is int.
 When byte and short values are used in
an expression, they are promoted to
int when the expression is evaluated.
 Therefore, int is often the best choice
when an integer is needed.
class MyFirstApp{
public static void main(String
args[]){
byte a = 4;
byte b = 5;
byte c = a+b;
Output???
System.out.println(c);
}
}
Floating-Point Types
Floating-pointnumbers, also known
as real numbers, are used when
evaluating expressions that require
fractional precision.
For example, calculations such as
square root, or transcendentals such
as sine and cosine, result in a value
whose precision requires a floating-
point type.
Characters
 Java uses Unicode to represent
characters.
 Since Java is designed to allow
programs to be written for worldwide
use, it makes sense that it would use
Unicode to represent characters.
Java char is a 16-bit type. The range
of a char is 0 to 65,536. There are no
negative chars.
Char type
 Although char is designed to hold
Unicode characters, it can also be used
as an integer type on which you can
perform arithmetic operations.
 Forexample, you can add two characters
together, or pre/post increment the
value of a character variable.
 Char is used to store a single character.
// 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);
}
}
Boolean Type
 Java has a primitive type, called
boolean, for logical values.
 It can have only one of two possible
values, true or false.
 boolean is also the type required by the
conditional expressions that govern the
control statements such as ‘if’ and
‘for’.
Assigning Values
 You can assign a value to a variable
in one of several ways including:
 assign a literal value after the equals
sign (x=12, isGood = true, etc.)
 assignthe value of one variable to
another (x = y)
 use an expression combining the two
(x = y + 43)
Exercise
int x = 34.5 ; s = y;
boolean boo = x;  byte b=3;
int g = 17;  byte v=b;
int y =g;  short n =12;
y=y + 10;  v = n;
short s;  byte k = 128;
Literals
Integer Literals - Octal
 1, 2, 3, and 42. These are all decimal values,
meaning they are describing a base 10
number.
 Two other bases that can be used in integer
literals are octal (base eight) and
hexadecimal (base 16).
 Octal values are denoted in Java by a
leading zero.
 value 09 will produce an error from the compiler,
since 9 is outside of octal’s 0 to 7 range.
class MyFirstApp{
public static void main(String args[]) {
int octnum1 = 011; //decimal 9
int octnum2 = 06; //decimal 6
System.out.println(octnum1 +octnum2);
}
}
Integer Literal - Hexadecimal
You signify 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.
Integer Literals - Binary
 Beginning with JDK 7, you can also
specify integer literals using binary.
 To do so, prefix the value with 0b or
0B.
 For example, this specifies the decimal
value 10 using a binary literal:
 int x = 0b1010;
class MyFirstApp{
public static void main(String args[]) {
int binaryNum1 = 0B11; //decimal 3
int binaryNum2 = 0B10; // decimal 2
System.out.println(binaryNum1 +binaryNum2);
}
}
Integer Literals – Underscore
separator
 Beginning with JDK 7, you can embed one or more
underscores in an integer literal.
 Doing so makes it easier to read large integer literals.
When the literal is compiled, the underscores are
discarded. For example, given
 int x = 123_456_789;
 the value given to x will be 123,456,789.
 Underscores can only be used to separate digits. They
cannot come at the beginning or the end of a literal. It
is, however, permissible for more than one underscore
to be used between two digits. For example, this is
valid:
 int x = 123___456___789;
Floating Point Literals –Standard
Notation
 Floating-point numbers represent decimal
values with a fractional component.
 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, 387.14159, and 0.6667
Floating Point Literals – Scientific
Notation
 Scientific notation uses a standard-
notation, floating-point number plus a
suffix that specifies a power of 10 by
which the number is to be multiplied.
 The exponent is indicated by an E or e
followed by a decimal number, which
can be positive or negative.
 Examples include 6.022E23, and 2e+100.
Floating Point Literals
 Floating-point literals in Java default to double
precision.
 To specify a float literal, you must append an F or f
to the constant.
 You can also explicitly specify a double literal by
appending a D or d.
 Doing so is, of course, redundant.
 The default double type consumes 64 bits of
storage, while the smaller float type requires only
32 bits.
Hexadecimal floating-point
literals
They must be in a form similar to scientific
notation, but a P or p, rather than an E or
e, is used.
 For example, 0x12.2P2 is a valid floating-
point literal. The value following the P,
called the binary exponent, indicates the
power-of-two by which the number is
multiplied.
 Therefore, 0x12.2P2 represents 72.5.
Floating Point Literals – Underscore
Separator
Beginning with JDK 7, you can embed
one or more underscores in a floating-
point literal. This feature works the
same as it does for integer literals.
It is also permissible to use
underscores in the fractional portion of
the number. For example,
 double num = 9_423_497.1_0_9; is legal.
In this case, the fractional part is .109.
Boolean Literal
 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.
Character Literals
A literal character is represented
inside a pair of single quotes.
All of the visible ASCII characters
can be directly entered inside the
quotes, such as 'a', 'z', and '@'.
String Literals
 Stringliterals in Java are specified by
enclosing a sequence of characters
between a pair of double quotes.
 "Hello World"
 "two\nlines"
 “ \"This is in quotes\“ “
 One important thing to note about Java
strings is that they must begin and end
on the same line.
Select from among the following, the valid name(s) for the
selected literal category.

(a) Arithmetic (b) logical (c) Escape sequences


(d) ASCII (e) Bitwise
Thank You!

You might also like