Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java
Variables are names (often called “identifiers”) for memory locations. Just as there are many different types of “information” in math or English, there are many types of data in computer memory Primitive data types are called this because they cannot be used to create “objects” Data types: Since numbers come in several flavors like whole numbers, fractional numbers, positive or negative, even very larger or very small numbers Numeric data types are: int for whole numbers (integers) Real numbers (float or double) Scientific notation numberes Java OOP Programming 2
Two data types related to text: 1. String – literally a “list” of characters, but with methods that contribute to manipulating each String, for example: toUpper( ) toLower( ) 2. char - holds only one character at a time 3. Unicode: a code or numeric representation that uses numbers to represent numbers, symbols, and letters. Includes over 65K characters for other languages, even Greek and mathematics symbols. Data TypeSize (mem- bytes) Range byte1 byteInteger from to 127 short2 bytesIntegers in the range from - 32,768 to 32,767 int4 bytesReally small to really large nos. long8 bytesHUGE numbers float4 bytes-3.4E-38 to 3.4E38 double8 bytes-1.7E-308 to 1.7E308 Java OOP Programming 3
Integer types hold “whole” numbers Basic declaration has syntax: int ; Declaration and Initialization looks like: int age = 15; // age is 15 Read as “age gets the value of 15” Java OOP Programming 4 Data TypeSize (mem- bytes) Range byte1 byteInteger from -128 to 127 short2 bytesIntegers in the range from - 32,768 to 32,767 int4 bytesReally small to really large nos. long8 bytesHUGE numbers
OUTPUT : We have made a journey of 105 miles. It took us 120 minutes. Our account balance is $-20 About days ago Columbus stood on this spot. // This program has variables of several of the integer types. public class IntegerVariables { public static void main(String[] args) { int checking; // Declare an int variable named checking. byte miles; // Declare a byte variable named miles. short minutes; // Declare a short variable named minutes. long days; // Declare a long variable named days. checking = -20; miles = 105; minutes = 120; days = ; System.out.print("We have made a journey of " + miles); System.out.println(" miles."); System.out.println("It took us " + minutes + " minutes."); System.out.println("Our account balance is $" + checking); System.out.print("About " + days + " days ago Columbus "); System.out.println("stood on this spot."); } Java OOP Programming 5
OUTPUT : We have made a journey of 105 miles. It took us 120 minutes. Our account balance is $-20 About days ago Columbus stood on this spot. // This program has variables of several of the integer types. public class IntegerVariables { public static void main(String[] args) { int checking; // Declare an int variable named checking. byte miles; // Declare a byte variable named miles. short minutes; // Declare a short variable named minutes. long days; // Declare a long variable named days. checking = -20; miles = 105; minutes = 120; days = ; System.out.print("We have made a journey of " + miles); System.out.println(" miles."); System.out.println("It took us " + minutes + " minutes."); System.out.println("Our account balance is $" + checking); System.out.print("About " + days + " days ago Columbus "); System.out.println("stood on this spot."); } Java OOP Programming 6 Single line comment Class header: public – can be access by other projects/classes Class – name of the container, keyword IntegerVariables – programmer created class name Left brace, begin class definition Header for main method, static defined page 55
public class IntegerVariables { public static void main(String[] args) { int checking;. Static: (Java Methods: as define on page 55,) “indicates that the method main is not called for any particular object: it belongs to the class as a whole.” A Java app can have only one main. Java OOP Programming 7 Header for main method,
OUTPUT : We have made a journey of 105 miles. It took us 120 minutes. Our account balance is $-20 About days ago Columbus stood on this spot. // This program has variables of several of the integer types. public class IntegerVariables { public static void main(String[] args) { int checking; // Declare an int variable named checking. byte miles; // Declare a byte variable named miles. short minutes; // Declare a short variable named minutes. long days; // Declare a long variable named days. checking = -20; miles = 105; minutes = 120; days = ; System.out.print("We have made a journey of " + miles); System.out.println(" miles."); System.out.println("It took us " + minutes + " minutes."); System.out.println("Our account balance is $" + checking); System.out.print("About " + days + " days ago Columbus "); System.out.println("stood on this spot."); } Java OOP Programming 8 Three process: Input, output, processing, Declaration of variables first Assignment second Output is last Data type: int (whole numbers) Variable name: checking Data type: byte, Variable name: miles Data Type: short, Variable minutes Data Type: long, Variable: days
OUTPUT : We have made a journey of 105 miles. It took us 120 minutes. Our account balance is $-20 About days ago Columbus stood on this spot. // This program has variables of several of the integer types. public class IntegerVariables { public static void main(String[] args) { int checking; // Declare an int variable named checking. byte miles; // Declare a byte variable named miles. short minutes; // Declare a short variable named minutes. long days; // Declare a long variable named days. checking = -20; miles = 105; minutes = 120; days = ; System.out.print("We have made a journey of " + miles); System.out.println(" miles."); System.out.println("It took us " + minutes + " minutes."); System.out.println("Our account balance is $" + checking); System.out.print("About " + days + " days ago Columbus "); System.out.println("stood on this spot."); } Java OOP Programming 9 Three process: Input, output, processing, Declaration of variables first Assignment second Output is last Assignments second; checking “gets value” 20 minutes gets 120 miles “gets” 105 Days “gets” the value of by the assignment op
OUTPUT : We have made a journey of 105 miles. It took us 120 minutes. Our account balance is $-20 About days ago Columbus stood on this spot. // This program has variables of several of the integer types. public class IntegerVariables { public static void main(String[] args) { int checking; // Declare an int variable named checking. byte miles; // Declare a byte variable named miles. short minutes; // Declare a short variable named minutes. long days; // Declare a long variable named days. checking = -20; miles = 105; minutes = 120; days = ; System.out.print("We have made a journey of " + miles); System.out.println(" miles."); System.out.println("It took us " + minutes + " minutes."); System.out.println("Our account balance is $" + checking); System.out.print("About " + days + " days ago Columbus "); System.out.println("stood on this spot."); } Java OOP Programming 10 Three process: Input, output, processing, Declaration of variables first Assignment second Output is last System.out.println( ) Method println displays strings from what is between ( )
Dollar amounts or precise measurements cannot be expressed as “int”egers Numbers that allow fractional values are called floating-point numbers Starting Out With Java, Page Two floating point data types: Float – single precision (4 bytes) store floating-pt numbers with 7 digits of accuracy Double – double precision Store floating-point with 15 digits of accuracy Code listing Sale.java Java OOP Programming 11
Code Examples: float number; number = 23.5; // error Correct example: float number; number = 23.5F; // ABOVE IS GOOD If you use “floating point” literals in your source code, Java is a “strongly typed” language, will not allow loss of precision Floating point literals are considered double Use an “F” capital letter to force (called a cast) conversion. Java OOP Programming 12
FLOATING POINTS expressed in E notation Pg 53 Java OOP Programming 13
boolean data type: Variables that can only hold the values of either True False Page 54 Java OOP Programming 14
Used to store character data, one letter, symbol, or numeric character Character literals are surrounded by single quotation marks. char myInitials = ‘J’; Page 55 Java OOP Programming 15
Math.pow(base, power) Math.sqrt(number) Java OOP Programming 16
Lesson 6 Project Java OOP Programming 17
Java OOP Programming 18
Variables & Data Types Data types and memory allocations (bytes) Integer values Output Source code example of Integer arithmetic and output Floating Point Data Types Math Pow Sqrt Other Lesson 6: Blue Pelican Java Java OOP Programming 19
Java OOP Programming 20