Download presentation
Presentation is loading. Please wait.
Published byEarl Goodwin Modified over 9 years ago
1
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
2
Terminology Primitive Data Type – a category of data. A description of how the computer will treat bits found in memory. Primitive Data Type – a category of data. A description of how the computer will treat bits found in memory. Variable – a named location in memory, treated as a particular data type, whose contents can be changed. Variable – a named location in memory, treated as a particular data type, whose contents can be changed. Constant – a named location in memory, treated as a particular type, whose contents cannot be changed. Constant – a named location in memory, treated as a particular type, whose contents cannot be changed. Declaration – the act of creating a variable or constant and specifying its type. Declaration – the act of creating a variable or constant and specifying its type. Literal – a hard-coded piece of data, part of the statement and not based on a variable or constant declaration. Literal – a hard-coded piece of data, part of the statement and not based on a variable or constant declaration. Operator – a symbol that describe how to manipulate data and variables in memory Operator – a symbol that describe how to manipulate data and variables in memory Expression – a combination of operators, variables, constants and/or literals that produces a resulting piece of data Expression – a combination of operators, variables, constants and/or literals that produces a resulting piece of data Assignment – copying the results of an expression into a variable. Assignment – copying the results of an expression into a variable. Statement – a program instruction telling the CPU what to do. (All statements end with semicolon). Statement – a program instruction telling the CPU what to do. (All statements end with semicolon).
3
Java Primitive Data types boolean -- true/false char -- Unicode character (good for internationalization) byte -- 8-bit signed integer short --16-bit signed integer int -- 32-bit signed integer long -- 64-bit signed integer float -- 32-bit floating point number double -- 64-bit floating point nbr
4
Identifiers Identifier = the name of a variable, constant, class, or method Identifier = the name of a variable, constant, class, or method Rules for using identifiers: Rules for using identifiers: An identifier must start with a letter, an underscore, or a dollar sign. An identifier must start with a letter, an underscore, or a dollar sign. An identifier cannot contain operators, such as +, -, and so on. An identifier cannot contain operators, such as +, -, and so on. An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,” for a list of reserved words). An identifier cannot be a reserved word. (See Appendix A, “Java Keywords,” for a list of reserved words). An identifier cannot be true, false, or null. An identifier cannot be true, false, or null. An identifier can be of any length. An identifier can be of any length.
5
Declaring Variables int x; // declares x to be an // integer variable; // integer variable; double radius; // declares radius to // be a double variable; // be a double variable; char a; // declares a to be a // character variable; // character variable; General format: datatype identifier; Declaring multiple variables of the same type: datatype identifier1, identifier2, identifier3;
6
Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a; General format: VariableIdentifier = expression; These expressions are all just Literals! Note: the = sign is an assignment operator. It is NOT a test for equality.
7
Declaring and Initializing in One Step int x = 1; int x = 1; double d = 1.4; double d = 1.4; float f = 1.4f; float f = 1.4f; char a = ‘a’; char a = ‘a’; Note: character literals are enclosed in single quotes NOTE: by default floating point literals are assumed to be doubles. If you want to assign a floating point literal to a float variable, you must append the “f” to the end of the number. declaration assignment
8
Constants final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final int SIZE = 3; The final modifier indicates that the identifier refers to a constant, not a variable. Constants must be initialized when declared.
9
Numeric Literals int i = 34; int i = 34; long l = 1000000; long l = 1000000; float f = 100.2f; or float f = 100.2F; float f = 100.2f; or float f = 100.2F; double d = 100.2d or double d = 100.2D; double d = 100.2d or double d = 100.2D;
10
Common Types of Operators Assignment= Assignment= Arithmetic+-*/% Arithmetic+-*/% Comparison== <> = != Comparison== <> = != Logical&&||!^ Logical&&||!^ equals AND OR NOT not equals modulus Exclusive OR
11
Modulus (remainder) Operator Modulus is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd. Suppose you know January 1, 2005 is Saturday, you can find that the day for February 1, 2005 is Tuesday using the following expression:
12
Common Types of Expressions Arithmetic Arithmetic Combine numeric data with arithmetic operators Combine numeric data with arithmetic operators Return a number Return a number Conditional Conditional Combine boolean values with logical operators Combine boolean values with logical operators Boolean values can be derived from comparison operators or boolean data values Boolean values can be derived from comparison operators or boolean data values Returna boolean value Returna boolean value
13
Arithmetic Expressions 1 + 1 1 + 1 x * y x * y 5 / 2 5 / 2 5 % 2 5 % 2 radius*radius*3.14159 radius*radius*3.14159
14
Sample Statements with Arithmetic Expressions //Compute the first area radius = 1.0; area = radius*radius*3.14159; //Compute the second area radius = 2.0; area = radius*radius*3.14;
15
Arithmetic Expressions is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
16
Shortcut Operators OperatorExampleEquivalent +=i+=8i = i+8 -=f-=8.0f = f-8.0 *=i*=8i = i*8 /=i/=8i = i/8 %=i%=8i = i%8
17
Increment and Decrement Operators x = 1; x = 1; x++;++x; x++;++x; x--;--x; x--;--x; y = 2 + x++; y = 2 + x++; y = 2 + ++x; y = 2 + ++x; y = 2 + x--; y = 2 + x--; y = 2 + --x; y = 2 + --x; Add 1 to x Subtract 1 from x X is incremented after adding to 2 X is incremented before adding to 2 X is decremented after adding to 2 X is decremented before adding to 2
18
Increment and Decrement Operators, cont.
19
Integer vs. Floating Point Division When performing operations involving two operands of different types, Java automatically converts the operand of a smaller range to the data type of the larger range. Example: 1/2 this will give 0 because both operands are integer 1.0/2 or 1/2.0 this will give 0.5 because the floating point literal is a double, so the integer literal (long) will be converted to a double; thus floating point division will take place.
20
Character Data Type char letter = 'A'; char letter = 'A'; char letter = '\u00041'; char letter = '\u00041'; char numChar = '4'; char numChar = '4'; Unicode representation Java uses Unicode instead of ASCII for character data representation
21
Character Escape Sequences Backspace \b Tab \t Linefeed \n Carriage return \r Backslash \\ Single quote \' Double quote \"
22
The boolean Data Type boolean lightsOn = true; boolean lightsOn = true; boolean lightsOn = false; boolean lightsOn = false; boolean test = 1==1; boolean test = 1==1; Returns true Returns true boolean test = 1==2; boolean test = 1==2; Returns false Returns false comparison expressions
23
The + symbol as concatenation operator System.out.println("The area is " + area + " for radius " + radius); String literals are enclosed in double quotes Here, the + is used to concatenate strings together
24
Boolean Operators Revisited Operator Name ! not && and || or ^ exclusive or
25
Truth Table for Operator !
26
Truth Table for Operator &&
27
Truth Table for Operator ||
28
Truth Table for Operator ^
29
Numeric Type Conversion Consider the following statements: byte i = 100; long k = i * 3 + 4; double d = i * 3.1 + k / 2;
30
Conversion Rules When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules: 1. If one of the operands is double, the other is converted into double. 2. Otherwise, if one of the operands is float, the other is converted into float. 3. Otherwise, if one of the operands is long, the other is converted into long. 4. Otherwise, both operands are converted into int.
31
Type Casting Implicit casting double d = 3; (type widening) double d = 3; (type widening) Explicit casting int i = (int)3.0; (type narrowing) int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated) int i = (int)3.9; (Fraction part is truncated) Cast operator
32
Operator Precedence Casting Casting ++, -- ++, -- *, /, % *, /, % +, - +, -, =>, => ==, !=; ==, !=; && && || || =, +=, -=, *=, /=, %= =, +=, -=, *=, /=, %= first last Parentheses can be used to override normal precedence
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.