Data types • Noautomatic coercions or conversions of conflicting types. • Primitive data type • Non primitive type
4.
Primitive data type •Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean
5.
Primitive data typecontd... • Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean char 16 0 to 65,536 boolean - true or false
6.
Integer literals • Whena literal value is assigned to a byte or short variable, no error is generated if the literal value is within the range of the target type. • An integer literal can always be assigned to a long variable. • long literal ? • you will need to explicitly tell the compiler that the literal value is of type long. • Do this by appending an upper- or lowercase L to the literal. • For example, 0x7ffffffffffffffL or 9223372036854775807L is the largest long. • An integer can also be assigned to a char as long as it is within range. • Decimal numbers cannot have a leading zero. Therefore signify a hexadecimal constant with a leading zero-x, (0x or 0X). • int x = 0b1010; //0B or 0b for binary • embed one or more underscores in an integer literal. cannot come at the beginning or the end of a literal • int x = 123_456_789; // x will be 123456789. The underscores will be ignored. • int x = 123 456 789; // x will be 123456789. The underscores will be ignored. • int x = 0b1101_0101_0001_1010; // x will be 54554
7.
Float literal • afloat literal, must append an F or f to the constant. • A double literal, must append a D or d to the constant. • When the literal is compiled, the underscores are discarded. • double num = 9_423_497.1_0_9; // 9423497.109. • double num = 9_423_497_862.0;
8.
Boolean Literals • Twological 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.
9.
Character Literals • Aliteral 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 '@'. • For characters that are impossible to enter directly, use escape sequences that allow you to enter the character • ' ' ' for the single-quote character itself and ' n' for the newline character. • For octal notation, use the backslash followed by the three-digit number. • For example, ' 141' is the letter 'a'. • enter a backslash-u ( u), then exactly four hexadecimal digits. • For hexadecimal example, ' u0061' is the ISO- Latin-1 'a' • ' ua432 ' is a Japanese Katakana character.
10.
String Literals • Stringliterals in Java are specified by enclosing a sequence of characters between a pair of double quotes. • Examples of string literals are "Hello World" "twonlines" " "This is in quotes""
11.
Variables • The variableis the basic unit of storage in a Java program. • A variable is defined by the combination of an identifier, a type, and an optional initializer. • In addition, all variables have a scope, which defines their visibility, and a lifetime. Declaring a Variable • In Java, all variables must be declared before they can be used. The basic form of a variable declaration is shown here: type identifier [ = value ][, identifier [= value ] …];
Scope • Although blockscan be nested, you cannot declare a variable to have the same name as one in an outer scope.
17.
Type Conversion andCasting • Ifthe two types are compatible, then Java will perform the conversion automatically. • For example, it is always possible to assign an int value to a long variable. • For instance, there is no automatic conversion defined from double to byte. • use a cast, which performs an explicit conversion between incompatible types. • Java’s Automatic Conversions: no explicit cast statement is required. • The two types are compatible. • The destination type is larger than the source type. • no automatic conversions from the numeric types to char or boolean. • performs an automatic type conversion when storing a literal integer constant into variables of type byte, short, long, or char.
18.
Casting Incompatible Types •what if you want to assign an int value to a byte variable? • This conversion will not be performed automatically, because a byte is smaller than an int. • This kind of conversion is sometimes called a narrowing conversion • explicitly making the value narrower so that it will fit into the target type. • To create a conversion between two incompatible types, you must use a cast. • A cast is simply an explicit type conversion. It has this general form: • (target-type) value int a; byte b; // … b = (byte) a;
19.
Casting Incompatible Types Conversionof int to byte. i and b 257 1 Conversion of double to int. d and i 323.142 323 Conversion of double to byte. d and b 323.142 67
20.
• Automatic TypePromotion in Expressions • Type Promotion Rules • First, all byte, short, and char values are promoted to int, as just described. • Then, if one operand is a long, the whole expression is promoted to long. • If one operand is a float, the entire expression is promoted to float. • If any of the operands are double, the result is double. byte a = 40; byte b = 50; byte c = 100; int d = a * b / c;
21.
Arrays • An arrayis a group of like-typed variables that are referred to by a common name. • Arrays of any type can be created and may have one or more dimensions. • A specific element in an array is accessed by its index.
Alternative Array DeclarationSyntax type[ ] var-name; int al[] = new int[3]; int[] a2 = new int[3]; char twod1[][] = new char[3][4]; char[][] twod2 = new char[3][4]; int[] nums, nums2, nums3; // create three arrays int nums[], nums2[], nums3[]; // create three arrays
27.
Introducing Type Inferencewith Local Variables • Recently, an exciting new feature called local variable type inference was added to the Java language. • In the past, all variables required an explicitly declared type, whether they were initialized or not. • Beginning with JDK 10, it is now possible to let the compiler infer the type of a local variable based on the type of its initializer, thus avoiding the need to explicitly specify the type. • Local variable type inference offers a number of advantages. • eliminating the need to redundantly specify a variable’s type when it can be inferred from its initializer. • It can simplify declarations in cases in which the type name is quite lengthy, such as can be the case with some class names.
28.
Introducing Type Inferencewith Local Variables double avg = 10.0; var avg = 10.0; Value of avg: 10.0 Value of var: 1 Value of k: -1 var myArray = new int[10]; // This is valid. var[] myArray = new int[10]; // Wrong var myArray[] = new int[10]; // Wrong var counter; // Wrong! Initializer required. var myArray = new int[10]; // This is valid. var myArray = new int[10]; // This is valid.
29.
Strings • String, isnot a primitive type String str = "this is a test"; System.out.println(str);
30.
Round a numberusing format public class Decimal { public static void main(String[] args) { double num = 1.234567; System.out.format("%.2f", num); } }
31.
Formatted output in Java classJavaFormat { public static void main(String args[]) { int x = 10; System.out.printf("Print integer: x = %dn", x); // print it upto 2 decimal places System.out.printf("Formatted with precision: PI = %.2fn", Math.PI); float n = 5.2f; // automatically appends zero to the rightmost part of decimal System.out.printf("Formatted to specific width: n = %.4fn", n); n = 2324435.3f; // width of 20 characters System.out.printf("Formatted to right margin: n = %20.4fn", n); } }