0% found this document useful (0 votes)
40 views30 pages

Chapter - 4 (IX)

Uploaded by

parasharaarna2
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)
40 views30 pages

Chapter - 4 (IX)

Uploaded by

parasharaarna2
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/ 30

Chapter – 4

Values and Data Types


Java character Set
•Characters are the smallest units (elements) of Java
language that are used to write Java tokens.
•These characters are defined by the Unicode character set.
• A character set in Java is a set of alphabets, letters, and
some special characters that are valid in java
programming language.
Tokens in java
• The Java compiler breaks the line of code into
text (words) is called Java tokens.
• These are the smallest element of the Java
programs.
• The Java compiler identified these words as
tokens.
• These tokens are separated by the delimiters.
• It is useful for compilers to detect errors.
public class Demo
{
public static void main(String args[])
{
System.out.println(“welcome");
}
}
in the above code, public, class, Demo, {, static, void,
main, (, String, args, [, ], ), System, ., out, println,
welcome, etc. are the Java tokens.
Keywords

• These are the pre-defined reserved words of


any programming language.
• Each keyword has a special meaning.
• It is always written in lower case.
01. abstract 02. boolean 03. byte 04. break 05. class

06. case 07. catch 08. char 09. continue 10. default

11. do 12. double 13. else 14. extends 15. final

16. finally 17. float 18. for 19. if 20. implements

21. import 22. instanceof 23. int 24. interface 25. long

26. native 27. new 28. package 29. private 30. protected

31. public 32. return 33. short 34. static 35. super

36. switch 37. synchronized 38. this 39. thro 40. throws

41. transient 42. try 43. void 44. volatile 45. while

46. assert 47. const 48. enum 49. goto 50. strictfp
Identifier
• Identifiers are used to name a variable,
constant, function, class, and array.
• It usually defined by the user.
• It uses letters, underscores, or a dollar sign as
the first character.
• The label is also known as a special kind of
identifier that is used in the goto statement.
• Remember that the identifier name must be
different from the reserved keywords.
There are some rules to declare
identifiers are:
• The first letter of an identifier must be a letter,
underscore or a dollar sign. It cannot start
with digits but may contain digits.
• The whitespace cannot be included in the
identifier.
• Identifiers are case sensitive.
Valid identifiers
• PhoneNumber
• PRICE radius
• a
• a1
• _phonenumber
• $circumference
• jagged_array
• 12radius //invalid
Literals
• In programming literal is a notation that represents a fixed
value (constant) in the source code.
• It can be categorized as an integer literal, string literal,
Boolean literal, etc.
• It is defined by the programmer. Once it has been defined
cannot be changed. Java provides five types of literals are as
follows:
– Integer
– Floating Point
– Character
– String
– Boolean
Literal Type

23 int

9.86 double

false, true boolean

'K', '7', '-' char

"javatpoint" String

null any reference type


Operators
• In programming, operators are the special symbol that tells
the compiler to perform a special operation.
• Java provides different types of operators that can be
classified according to the functionality they provide.
• There are eight types of operators in Java, are as follows:
– Arithmetic Operators
– Assignment Operators
– Relational Operators
– Unary Operators
– Logical Operators
– Ternary Operators
– Bitwise Operators
– Shift Operators
Operator Symbols

Arithmetic +,-,/,*,%

Unary ++ , - - , !

Assignment = , += , -= , *= , /= , %= , ^=

Relational ==, != , < , >, <= , >=

Logical && , ||

Ternary (Condition) ? (Statement1) : (Statement2);

Bitwise &,|,^,~

Shift << , >> , >>>


Separators
• The separators in Java is also known as
punctuators. There are nine separators in
Java, are as follows:
• separator <= ; | , | . | ( | ) | { | } | [ | ]
• Square Brackets []: It is used to define array elements. A pair of
square brackets represents the single-dimensional array, two
pairs of square brackets represent the two-dimensional array.
• Parentheses (): It is used to call the functions and parsing the
parameters.
• Curly Braces {}: The curly braces denote the starting and ending
of a code block.
• Comma (,): It is used to separate two values, statements, and
parameters.
• Assignment Operator (=): It is used to assign a variable and
constant.
• Semicolon (;): It is the symbol that can be found at end of the
statements. It separates the two statements.
• Period (.): It separates the package name form the sub-
packages and class. It also separates a variable or method from
a reference variable.
Comments:
• Comments allow us to specify information about the
program inside our Java code.
• Java compiler recognizes these comments as tokens
but excludes it form further processing.
• The Java compiler treats comments as whitespaces.
• Java provides the following two types of comments:
• Line Oriented: It begins with a pair of forwarding
slashes (//).
• Block-Oriented: It begins with /* and continues until it
founds */.
Data Type Default Value Default size

boolean false 1 bit

char '\u0000' 2 byte

byte 0 1 byte

short 0 2 byte

int 0 4 byte

long 0L 8 byte

float 0.0f 4 byte

double 0.0d 8 byte


Boolean Data Type
• The Boolean data type is used to store only two
possible values: true and false. This data type is
used for simple flags that track true/false
conditions.
• The Boolean data type specifies one bit of
information, but its "size" can't be defined
precisely.
• Example:
• Boolean one = false
Byte Data Type
• The byte data type is an example of primitive data type.
It is an 8-bit signed two's complement integer. Its value-
range lies between -128 to 127 (inclusive). Its minimum
value is -128 and maximum value is 127. Its default value
is 0.
• The byte data type is used to save memory in large
arrays where the memory savings is most required. It
saves space because a byte is 4 times smaller than an
integer. It can also be used in place of "int" data type.
• Example:
• byte a = 10, byte b = -20
Short Data Type
• The short data type is a 16-bit signed two's
complement integer. Its value-range lies between -
32,768 to 32,767 (inclusive). Its minimum value is -
32,768 and maximum value is 32,767. Its default
value is 0.
• The short data type can also be used to save
memory just like byte data type. A short data type
is 2 times smaller than an integer.
• Example:
• short s = 10000, short r = -5000
Int Data Type
• The int data type is a 32-bit signed two's complement
integer. Its value-range lies between - 2,147,483,648
(-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its
minimum value is - 2,147,483,648and maximum value
is 2,147,483,647. Its default value is 0.
• The int data type is generally used as a default data
type for integral values unless if there is no problem
about memory.
• Example:
• int a = 100000, int b = -200000
Long Data Type
• The long data type is a 64-bit two's complement integer.
Its value-range lies between -
9,223,372,036,854,775,808(-2^63) to
9,223,372,036,854,775,807(2^63 -1)(inclusive). Its
minimum value is - 9,223,372,036,854,775,808and
maximum value is 9,223,372,036,854,775,807. Its
default value is 0. The long data type is used when you
need a range of values more than those provided by int.
• Example:
• long a = 100000L, long b = -200000L
Float Data Type
• The float data type is a single-precision 32-bit
IEEE 754 floating point.Its value range is
unlimited. It is recommended to use a float
(instead of double) if you need to save memory
in large arrays of floating point numbers. The
float data type should never be used for precise
values, such as currency. Its default value is 0.0F.
• Example:
• float f1 = 234.5f
Double Data Type
• The double data type is a double-precision 64-
bit IEEE 754 floating point. Its value range is
unlimited. The double data type is generally
used for decimal values just like float. The
double data type also should never be used
for precise values, such as currency. Its default
value is 0.0d.
• Example:
• double d1 = 12.3
Char Data Type
• The char data type is a single 16-bit Unicode
character. Its value-range lies between '\
u0000' (or 0) to '\uffff' (or 65,535
inclusive).The char data type is used to store
characters.
• Example:
• char letterA = 'A'
Unicode System
• Unicode is a universal international standard
character encoding that is capable of representing
most of the world's written languages. Why java
uses Unicode System?
• Before Unicode, there were many language
standards: ASCII (American Standard Code for
Information Interchange) for the United States.
• ISO 8859-1 for Western European Language.
• KOI-8 for Russian.
• GB18030 and BIG-5 for chinese, and so on.
Problem
• This caused two problems: A particular code value
corresponds to different letters in the various language
standards.
• The encodings for languages with large character sets
have variable length.Some common characters are
encoded as single bytes, other require two or more byte.
• Solution
• To solve these problems, a new language standard was
developed i.e. Unicode System. In unicode, character
holds 2 byte, so java also uses 2 byte for characters.
lowest value:\u0000 highest value:\uFFFF

You might also like