Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

An overview of Java primitive data types, their storage and range of values, variable declaration and assignment, and expression evaluation. It covers the different categories of data in Java, the eight primitive data types, and their corresponding literals. The document also explains the concept of variables, their declaration and initialization, and the use of assignment statements and expressions.

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

dreamingofyou
dreamingofyou 🇬🇧

4.5

(15)

233 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Primitive Data Types
15-110 Summer 2010
Margaret Reid-Miller
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

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