You are on page 1of 3

Chapter 2: Fundamental Data Types Chapter Goals To declare and initialize variables and constants To understand the properties

and limitations of integers and floating-point numbers To appreciate the importance of comments and good code layout To write arithmetic expressions and assignment statements To create programs that read and process inputs, and display the results To learn how to use the Java String type Contents 2.1 Variables 2.2 Arithmetic 2.3 Input and Output 2.4 Problem Solving: First Do It By Hand 2.5 Strings 2.1 Variables Most computer programs hold temporary values in named storage locations Programmers name them for easy access There are many different types (sizes) of storage to hold different things You declare a variable by telling the compiler: What type (size) of variable you need What name you will use to refer to it

Type Casting P65 int points; double distance; int points = (int) distance;

Variables VARIABLE DECLARATION syntax typeName variableName = value; or typeName variableName; Variable is a storage location with a name Declaration of a value specify a DataType for the compiler (ie: double total;) Initialize a value specify the value stored in the variable (ie: double total = 0;) A variable must be declared and initialized before its used (ie: int variableName = 10;) Java compiles statements in order (top to bottom) NAMING CONVENTIONS syntax variableName CONSTANT_NAME ClassName Must start with a letter or underscore (_) Remaining characters must be letters, numbers, or underscores Use uppercase letters to denote word boundaries (ie: cansPerPack) Variable names are case sensitive and start with lower case letters Camel case naming convention for variables (ie: variableName) Class names start with uppercase letter (ie: ClassName) Reserved words words reserved by Java (ie: class, double) CONSTANTS syntax final typeName variableName = expression; Constants fixed values that never change; all capital letters with underscore between words (ie: TAX_RATE) final you cannot change the value of the variable (ie: final double TAX_RATE = 8.25;)

PRIMATIVE TYPES Four integer types (byte, short, int, and long) 9 ,1, 0 Two floating-point types (float and double) 9.43

One character type (char) a One Boolean type (boolean) true/false

NUMBER TYPES Number literals are floating point numbers if they have a decimal, otherwise its an integer int (integer) whole numbers with no factional parts (ie: 9/2 = 4) Overflow computation yields a value that is outside of integer range; result is truncated double (decimal) a number with a fractional part (ie: 9/2.0 = 4.5) Floating-point numbers also known as real numbers INTEGER DIVISION (/) and REMAINDER (%) Integer division If both arguments of / are integers, then the remainder is discarded (ie: 9/2 = 4) Modulus (remainder) only computes the remainder (ie: 7 % 4 = 3) N % 2 is 0 if n is even, 1 or -1 if n is odd POWER and ROOTS Math.sqrt method to take the square root of a number Math.pow(x, n) to compute the power of a number ASSIGNMENT STATEMENT syntax variableName = value; Assignment statement stores a new value in a variable, replacing the previously assigned value The value on the right of the '=' sign is stored to a memory location represented by a variable on the left Assignment Operator (=) (ie: total = total * BOTTLE_VOLUME;) ARITHMETIC OPERATORS Operand arithmetic expressions (ie: +, , *, /) Expression the combination of variables, literals, operators, and/or methods (ie: (a + b) / 2) Order of Precedence: byte short int long float double INCREMENT and DECREMENT count++ increase the value of the variable count by 1. (ie: count = count +1) count decrease the value of the variable count by 1. (ie: count = count 1) Literal characters in quotes String 2.3 INPUT / OUTPUT Package a collection of classes with a related purpose. import java.util.*; import java.util.Scanner; Prompt a message that tells the user which input is expected Scanner a class object that allows program to read keyboard input. Scanner in = new Scanner(System.in); nextInt method used to read an integer value int bottles = in.nextInt(); nextDouble method used to read an double value double bottles = in.nextDouble(); nextLine method used to read an string value String bottles = in.nextLine();

2.5 STRINGS

String are sequences of characters. Characters are letters, numbers, punctuation, spaces, etc. Literals character sequence enclosed in quotes string Example: String name = "Eric"; Empty String is a string of length 0; contains no characters and is written Example: String empty = ""; Length is the number of characters in a String Example: int n = name.length();

Concantenation (+) joining two strings and other operators Example: : int n = 7;
String fName = Eric; String lName = Ellsworth; String name = fName + lName + n;

Note: Whenever one of the arguments of the + operator is a string, the other argument is converted to a string. STRING INPUT next () of the Scanner class only reads a string containing a single word. If the user types two words, then only the first word is read. Use nextLine() instead to read the entire line of user input. ESCAPE SEQUENCES Quotion mark in literal string Example: String n = He said \Hello\; Newline Character (\n) Example: System.out.print("*\n**\n***\n"); STRINGS AND CHARACTERS
H H

is a character, a value of type char is a string containting a single character, a value of type String returns a char value from a string starting the first position 0.
char start = name.charAt(0); char end = name.charAt(3); starts at E; position 0 ends at c; position 3

charAt()

Example: String name = Eric;

2.5.6 SUBSTRING

You might also like