BITS Pilani
Hyderabad Campus
BITS Pilani
Hyderabad Campus
Todays Agenda
Write a simple Java program Identifiers, Variables, and Constants Primitive Data Types
Byte, short, int, long, float, double, char, boolean
Operators
+, -, *, /, %, +=, -=, *=, /=, %=, ++, --
Example 2.1: Computing the area of a Circle. This program reads the radius from the keyboard and computes the area of the circle. Then display the result on screen. Math: area= xradius2
Identifiers --Naming
Naming variable, constant, method, object, class, files
An identifier must start with a letter, an underscore '_', a dollar sign, or '$' . An identifier cannot contain operators, such as +, -, and so on. Case-sensitive (like C/C++) Not start with a digital number An identifier cannot be a reserved word. An identifier cannot be true, false, or null.
Identifiers --Naming
Reserved keyword
Identifiers --Naming
Naming variable, constant, method, object, class, files
Underscore, letters or $ Case-sensitive (like C/C++) Not start with a number No illegal character (operators), such as -, +, *, <,> No reserved keywords
Conventional
All capitals are constant First capital is class name Variables and objects start with low case
Declaring Variables
int x; double radius; char a; // Declare x to be an integer variable; // Declare radius to be a double //variable; // Declare a to be a character variable;
Assignment Statements
x = 1; radius = 1.0; a = 'A'; // Assign 1 to x; // Assign 1.0 to radius; // Assign 'A' to a;
Declaring and Initializing in One Step int x = 1; double d = 1.4; float f = 1.4;
Constants
final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final int SIZE = 3;
byte
Byte data type is a 8-bit signed two's complement integer. Minimum value is -128 (-2^7) Maximum value is 127 (inclusive)(2^7 -1) Default value is 0 Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int. Example : byte a = 100 , byte b = -50
short
Short data type is a 16-bit signed two's complement integer. Minimum value is -32,768 (-2^15) Maximum value is 32,767(inclusive) (2^15 -1) Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int Default value is 0. Example : short s= 10000 , short r = -20000
int
Int data type is a 32-bit signed two's complement integer. Minimum value is - 2,147,483,648.(-2^31) Maximum value is 2,147,483,647(inclusive).(2^31 -1) Int is generally used as the default data type for integral values unless there is a concern about memory. The default value is 0. Example : int a = 100000, int b = -200000
long
Long data type is a 64-bit signed two's complement integer. Minimum value is -9,223,372,036,854,775,808.(-2^63) Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1) This type is used when a wider range than int is needed. Default value is 0L. Example : int a = 100000L, int b = -200000L
Float
Float data type is a single-precision 32-bit IEEE 754 floating point. Float is mainly used to save memory in large arrays of floating point numbers. Default value is 0.0f. Float data type is never used for precise values such as currency. Example : float f1 = 234.5f
double
double data type is a double-precision 64-bit IEEE 754 floating point. This data type is generally used as the default data type for decimal values. generally the default choice. Double data type should never be used for precise values such as currency. Default value is 0.0d. Example : double d1 = 123.4
boolean
boolean data type represents one bit of information. There are only two possible values : true and false. This data type is used for simple flags that track true/false conditions. Default value is false. Example : boolean one = true
char
char data type is a single 16-bit Unicode character. Minimum value is '\u0000' (or 0). Maximum value is '\uffff' (or 65,535 inclusive). Char data type is used to store any character. Example . char letterA ='A'
Representation
byte, int, long, and short can be expressed in decimal(base 10),hexadecimal(base 16) or octal(base 8) number systems as well. Prefix 0 is used to indicates octal and prefix 0x indicates hexadecimal when using these number systems for literals. For example: int decimal = 100; int octal = 0144; int hexa = 0x64;
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
Number literals
int i = 34; long l = 1000000; float f = 100.2f; or float f = 100.2F; double d = 100.2d or double d = 100.2D;
Symbol
+
Operation
Addition
Example
45 + 5 = 50
Subtraction
657 57 = 600
/
%
Division
Remainder
10 / 3 = 3
10 % 3 = 1
BITS Pilani, Hyderabad Campus
Precedence Rules
1. Evaluate all subexpressions in parentheses 2. Evaluate nested parentheses from the inside out 3. In the absence of parentheses or within parentheses
a. Evaluate *, /, or % before + or b. Evaluate sequences of *, /, and % operators from left to right c. Evaluate sequences of + and operators from left to right
Precedence Examples
Example 1
6 + 37 % 8 / 5 is the same as 6 + ((37 % 8) / 5) = 6 + ( 5 / 5) = 7
Example 2
6 + 37 % (8 / 5) = 6 + 37 % 1 = 6+0=6
Reading Integers
import CSLib.*; Inputbox in; int r; in = newInputBox(); in.setPrompt(Enter an radius: ); r = in.readInt();
Real Numbers
Numbers with fractional parts 3.14159, 7.12, 9.0, 0.5e001, -16.3e+002 Declared using the type double
double pricePerPound = 3.99; taxRate = 0.05; shippingCost = 5.55;
Symbol
+ -
Operation
Addition Subtraction
Example
4.50e01 + 5.30e00 = 5.03e01 6.57e02 5.7oe01 = 6.00e02
*
/
Multiplication
Division
Shortcut operators
Operator += -= *= /= %= Example i+=8 f-=8.0 i*=8 i/=8 i%=8 Equivalent i = i+8 f = f-8.0 i = i*8 i = i/8 i = i%8
Take the value of the rvalue and store it in the lvalue. The rvalue is any constant, variable or expression. The lvalue is named variable.
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
Simple Arithmetic
public class Example { public static void main(String[] args) { int j, k, p, q, r, s, t; j = 5; k = 2; p = j + k; q = j - k; r = j * k; s = j / k; t = j % k; System.out.println("p = " + p); System.out.println("q = " + q); > java Example System.out.println("r = " + r); p = 7 q = 3 System.out.println("s = " + s); r = 10 System.out.println("t = " + t); s = 2 } t = 1 } >
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
Shorthand Operators
public class Example { public static void main(String[] args) { int j, p, q, r, s, t; j = 5; p = 1; q = 2; r = 3; s = 4; t = 5; p += j; q -= j; r *= j; s /= j; t %= j; System.out.println("p = " + p); System.out.println("q = " + q); > java Example System.out.println("r = " + r); p = 6 System.out.println("s = " + s); q = -3 System.out.println("t = " + t); r = 15 s = 0 } t = 0 } >
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
A Change of Topic
Casting
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus
int i;
l i long 64 bit
long l;
int 32 bit
char 16 bit
byte 8 bit
short 16 bit
int 32 bit
long 64 bit
float 32 bit
double 64 bit
Illegal b = l; l = f; c = s;
OK l = b; i = c; f = l
Example
public class Example { public static void main(String[] args) { byte b; int i = 257; double d = 323.566; System.out.println(Conversion of int to byte); b = (byte) i; System.out.println(i and b+ I + + b);
} }
Summary
Java defines 8 simple data types : byte, short, int, long, char, float, double and boolean grouped into four categories like Integers, Floating-Point numbers, Characters and Boolean