
Java Primitive Data Types and Literals
Learn about Java's primitive data types (integer, floating-point, boolean, and character) and literals. Explore how these data types store different kinds of values in Java programming.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
BCA 504 : Java Programming
Javas Primitive Types Java contains two general categories of built-in data types: object-oriented and non-object- oriented. Java s object-oriented types are defined by classes. However, at the core of Java are eight primitive (also called elemental or simple) types of data. Data Type Size Description byte 1 byte Stores whole numbers from -128 to 127 short 2 bytes Stores whole numbers from -32,768 to 32,767 int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647 long 8 bytes Stores whole numbers from - 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits boolean 1 bit Stores true or false values char 2 bytes Stores a single character/letter or ASCII values
Numbers Primitive number types are divided into two groups: Integer types stores whole numbers, positive or negative (such as 123 or -456), without decimals. Valid types are byte, short, int and long. Which type you used depends on the numeric value. 1)Integer Types 1. Byte : The byte data type can store whole numbers from -128 to 127. Ex: byte number = 100; 2. Short : The short data type can store whole numbers from -32768 to 32767. Ex: short number = 10000; 3. Int: The int data type can store whole numbers from -2147483648 to 2147483647. Ex: int number = 100000; 4. Long: The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. Ex: long number = 15000000000L;
2) Floating Point Types Floating point types represents numbers with a fractional part, containing one or more decimals. There are two types float and double. 1. Float: The float data type can store fractional numbers from 3.4e 038 to 3.4e+038. Ex: float number = 5.75f; 2. Double: The double data type can store fractional numbers from 1.7e 308 to 1.7e+308. Ex: double number = 19.99d; 3) Booleans: A boolean data type is declared with the boolean keyword and can only take the values true or false. Ex: boolean isJavaFun = true; 4) Characters: The char data type is used to store a single character. The character must be surrounded by single quotes, like A' or e . Ex: char myGrade = A ;
Literals Literals in Java are a sequence of characters (digits, letters, and other characters) that represent constant values to be stored in variables. Java language specifies five major types of literals. They are: Integer literals Floating point literals Character literals String literals Boolean literals Each of them has a type associated with it. The type describes how the values behave and how they are stored. Integer literals: An integer literal refers to a sequence of digits. There are three types of integers, namely, decimal integer, octal integer and hexadecimal integer Decimal integer literals consist of a set of digits, 0 through 9, preceded by an optional minus sign. Valid examples of decimal integer constants are: 123, -321, 0654321 An octal integer literal consists of any combination of digits from the set 0 through 7, with a leading 0. Some examples of octal integer are: 037, 00435, 0551 A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer
Floating point literals: Integer numbers are inadequate to represent quantities that vary continuously, such as distances, heights, temperatures, prices, and so on. These quantities are represented by numbers containing fractional parts like 17.548. Such numbers are called real (or floating point) constants. Single Character literals: A single literal contains a single character enclosed within a pair of single quote marks. Examples of character constants are: '5', 'X', ; String literals: A string literal is a sequence of characters enclosed between double quotes. The characters may be alphabets, digits, special characters and blank spaces. Examples are: "Hello Java", "1997", "WELL DONE", "?..!", "5+3", "X". Boolean literals: Boolean literals include true or false values. Ex: boolean MyValue = true;
Variable Variables are containers for storing data values. In Java, there are different types of variables, for example: int, float, String, char, boolean Declaring(creating) variables Type variable = value; Ex: 1)int number = 10; 2) String name = BBHC ; 3)final int num = 5; num = 20; // it will generate an error 4) int a = 2, b = 4, c ;
Dynamic Initialization Java allows variables to be initialized dynamically, using any expression valid at the time the variable is declared. For example class DynInit { public static void main (String args[ ]) { double radius=4, height=5; // dynamically initialize volume double volume=3.1416 * radius * height; System.out.println( Volume is + volume); } }
Scope and lifetime of variables Java allows variables to be declared within any block. A block is begun with an opening curly brace and ended by a closing curly brace. A block defines a scope. Thus, each time you start a new block, you are creating a new scope. It determines the lifetime of those objects. Many other computer languages define two general categories of scopes: global and local. Although supported by Java. The most important scopes in Java are those defined by a class and those defined by a method. The scope defined by a method begins with its opening curly brace. if that method has parameters, they too are included within the method s scope.
class ScopeDemo { public static void main(String args[]) { int x; // Known to all code within main x=10; if (x = = 10) { //start new scope int y = 20; // known only to this block // x and y both known here. System.out.println( x and y: + x + + y); x= y * 2; } } // y = 100; //Error! Y not known here // x is still known here. System.out.println( x is +x); }
Operators Operator is a symbol that tells the computer to perform certain mathematical or logical manipulations Java operators can be classified into a number of related categories as below: Arithmetic operators I. Relational operators II. Logical operators III. IV. Assignment operators Increment and decrement operators V. VI. Conditional operators. VII. Bitwise operators VIII. Special operators
Arithmetic operators Java provides all the basic arithmetic operators. These can operate on any built-in numeric data type of Java. We cannot use these operators on boolean type. Operator Meaning + Addition or unary plus - Subtraction or unary minus * Multiplication / Division % Modulo division Arithmetic operators are used as shown below: a-b a*b a%b a+b a/b -a*b Here a and b may be variables or constants and are known as operands.
Integer Arithmetic: When both the operands in a single arithmetic expression such as a+ b are integers, the expression is called an integer expression, and the operation is called integer arithmetic Real Arithmetic: An arithmetic operation involving only real operands is called real arithmetic. Mixed-mode Arithmetic: When one of the operands is real and the other is integer, the expression is called a mixed-mode arithmetic expression. Increment and Decrement Operators These are the increment and decrement operators: ++ and -- The operator + + adds 1 to the operand while - - subtracts 1. Both are unary operators and are used in the following form. ++m; or m++; is equivalent to m = m + 1; --m; or m--; is equivalent to m = m - 1;
Example for Increment and Decrement operators int a = 5, b = 10; System.out.println(++a); //output is: 6 System.out.println(b++); //output is:10 System.out.println(b); // output is: 11 System.out.println(--a); // output is:5 System.out.println(b--); //output is: 11 System.out.println(b); //output is: 10
Relational and Logical Operators Operator Meaning < Is less than <= is less than or equal to > is greater than >= is greater than or equal to = = is equal to != is not equal to Logical Operators: In addition to the relational operators, Java has three logical operators Operator Meaning && Logical AND || Logical OR ! Logical Not