Docsity

Java Primitive Data Types and Variables, Study notes of Java Programming

Partial preview of the text

Download Java Primitive Data Types and Variables and more Study notes Java Programming in PDF only on Docsity!

Primitive Data Types

15-110 Summer 2010

Margaret Reid-Miller

Data Types

• Data stored in memory is a string of bits (0 or 1).

• What does 1000010 mean?

'B'?

9.2E-44?

• How the computer interprets the string of bits

depends on the context.

• In Java, we must make the context explicit by

specifying the type of the data.

Common Primitive Types

Type Description Example of Literals int integers (whole numbers) 42 , 60634 , -8, 0 double real numbers 0.039, -10.2, 4.2E+ char single characters 'a', 'B', '&', '6' boolean logical values true, false

Numbers

Type Storage Range of Values byte 8 bits -128 to 127 short 16 bits -32,768 to 32, int 32 bits -2,147,483,648 to 2,147,483, long 64 bits -9x 18 to 9x 18 float 32 bits ± 10

  • to ± 10 38 , 7 significant digits double 64 bits ± 10

to ± 10 308 , 15 significant digits

Variable Declaration

• Before you can use a variable, you must

declare its type and name.

• You can declare a variable only once in a

method.

• Examples:

int numDimes; double length; char courseSection; boolean done; String lastName;

Declaring Variables

  • Declaring a variable instructs the compiler to set aside a portion of memory large enough to hold data of that type. int count; double length; count length
  • No value has be put in memory yet. That is, the variable is undefined.

Re-Assigning Variables

  • A variable must be declared exactly once.
  • A variable can be assigned and re-assigned values many times after it is declared. Example: int x; x = 4; System.out.println(x); // prints 4 x = x + 1; System.out.println(x); // prints 5

Declaration/Initialization

• Variables can be declared and initialized in

one statement:

Examples:

int numDimes = 4; double length = 52.3; char courseSection = 'J'; boolean done = true; String lastName = "Reid-Miller"; int count = 3 + 2;

Division and Modulo

int a = 40; double x = 40.0; int b = 6; double y = 6.0; int c; double z; c = a / b; 6 c = a % b; 4 z = x / y; 6.66666667 c = b % a; 6 c = b / a; 0 c = 0 % a; 0 z = y / x; 0.15 c = b % 0; error c = 0 / a; 6 c = a / 0; error

  • The operators *, /, % are evaluated before the operators +, - because *, /, % have higher precedence than +, -. Example: 2 + 4 * 5
  • To change the order use parentheses: Example: (2 + 4) * 5 evaluates to ______

Operator Precedence

Other operators

  • Assignment operators : =, +=, -=, *=, /=, %= Example:
    • Shortcut for x = x + 2; is x += 2; (“add 2 to x”)
    • Shortcut for y = y * 3; is y *= 3; (“multiply y by 3”)
  • Increment / Decrement operators: ++, --
    • Shortcut for x = x + 1; is x++; (“increment x”)
    • Shortcut for y = y - 1; is y--; (“decrement y”)

Data Conversion

  • Widening conversions convert data to another type that has the same or more bits of storage. E.g. , - short to int, long (safe) - int to long (safe) - int to float, double (magnitude the same but can lose precision)
  • Narrowing conversions convert data to another type that has the fewer bits of storage and/or can lose information. E.g. , - double or float to any integer type - double to float

Mixing Types

  • Conversions are done on one operator at a time in the order the operators are evaluated. 3 / 2 * 3.0 + 8 / 3 5. 2.0 * 4 / 5 + 6 / 4.0 3.

Mixing Types

  • String concatenation has the same precedence as + - and is evaluated left to right. 1 + "x" + 4 "1x4" "2+3=" + 2 + 3 "2+3=23” 1 + 2 + " 3 " " 33 ” "23=" + 2 * 3 "23=6" 4 - 1 + "x" "3x" "x" + 4 - 1 error