primitive data types and arithmetic operations

Click here to load reader

Upload: chester-lowery

Post on 30-Dec-2015

29 views

Category:

Documents


2 download

DESCRIPTION

Primitive Data Types and Arithmetic Operations. CS0007: Introduction to Computer Programming. Review. The console or console screen is… a screen where the input and output is simple text. When using the console for output, it is said that you are using the… standard output device . - PowerPoint PPT Presentation

TRANSCRIPT

PowerPoint Presentation

CS0007: Introduction to Computer ProgrammingPrimitive Data Types and Arithmetic Operations1ReviewThe console or console screen is a screen where the input and output is simple text.When using the console for output, it is said that you are using the standard output device. The Java Application Programmer Interface (API) is a standard library of prewritten classes for performing specific operations.print print some text to the screenprintln print some text to the screen and add a new line character at the end (go to the next line).

2Reviewprint and println are contained in theout objectThe out object is contained in theSystem classEscape Sequencesallow a programmer to embed control characters or reserved characters into a string. In Java they begin with a backslash and then are followed by a character.A variable isa named storage location in the computers memory.Variables have 3 characteristics:Name the name we use in our code to refer to the memory location.Type characteristic that defines what legal values the variable can hold.Value what is actually held inside of the memory location.

3ReviewThe name of the variable should describe what it holds, this producesself-documenting programs.A variable declaration is a statement that tells the compiler the variables name, and what data type it is.You MUST declare a variable before you can use it.An assignment statementstores a value on the right side of the = to the variable on the left.A Literal is..a value that is explicitly written in the code of the program. When a variable is used NOT on the left hand side of a assignment statement, the ________________ is used.value stored in the variable

4Primitive Data TypesData Type defines what legal values the variable can hold.There are many types of data, you can even define your own type.int is known as a primitive data type.A primitive data type is a built-in data type that is a basic building block for all other types and DO NOT create objects.byte, short, int, long are all integer data types.float and double are floating-point (decimal) data types.

5Primitive Numeric Data TypesData TypeSizeRangebyte1 byteIntegers in the range of -128 to +128short2 bytesIntegers in the range of -32,768 to +32,767int4 bytesIntegers in the range of -2,147,483,648 to +2,147,483,647long8 bytesIntegers in the range of -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807float4 bytesFloating-point numbers in the range of 3.4x10-38 to 3.4x1038 with 7 digits of accuracy double8 bytesFloating-point numbers in the range of 1.7x10-308 to 1.7x10308 with 15 digits of accuracy 6Notes on Primitive Numeric TypesYou want to choose a type based on what the variable should doWhy would you not use double for a variable that counts the number of times the user clicks on something?Why would you not use int for a variable that calculates the exact weight a bridge can hold?One good thing about types in Java is that the sizes of these variables are the same on EVERY computer.Not the case in other languages.Other languages allow for bigger integers or unsigned integers, but Java does not have primitive types for this.Instead they have a BigInteger class.The BigInteger class allows the programmer to use all the arithmatic operations allowed by the primitive types on integers outside of the range of the primitive types.Similarly, there is a BigDecimal class.Probably not needed in this class7Integer Types ExampleNew Topics:int typebyte typeshort typelong typeString Concatenation Operator (+) String Concatenation is the process of appending to the end of a string.In Java, the concatenation operator allows you to concatenate strings with other string or simple variables of primitive type (Lines 15-18).L suffixAll integer literals are considered to be of type int, so you must specify that an integer literal is of type long if it is out of the range of type int.You can do this by putting the character L after the long literal (line 13).8Floating-Point Data TypesSometimes you may need to use fractional numbers in your program. In programming terminology, these are called floating-point numbers.Java provides two primitive floating-point data types:floatdoubleMost programmers tend to use double instead of float when dealing with floating-point numbers, unless for some reason the programmer needs to reduce the amount of memory a program takes up. This is done for multiple reasons:On some modern day processors, operations done on data of type double is actually faster than data of type float.Most methods that return floating-point results, return type double.Example: CosineNote, for similar reasons, programmers usually use int for integer data over the other integer types.

9Floating-Point Types ExampleNew Topics:float typedouble typeF suffixAll floating-point literals are considered to be of type double, so you must specify that an floating-point literal is of type float if it being assigned to a variable of type float.You can do this by putting the character F after the long literal (line 13).10Boolean Data TypeJava provides a data type that can either be one of two values: true or false. This is called boolean.Right, now this may not seem useful, but the boolean type will become very important later on.Just remember the following things about boolean:boolean variables can only hold the values of true or false.You cannot copy the values of boolean variables into any variables of other types.11Boolean Type ExampleNew Topics:boolean typetrue reserved wordfalse reserved word

12Character Data TypeJava also provides a data type for holding a single character called char.Character literals are surrounded by single quotes (')Characters are stored in memory as numbers, the program only knows it is a character and not a number by the type.Java uses Unicode, which is a standard set of numbers used as codes to represent characters. Each number representing a character requires two bytes, so the char data type takes up two bytes in memory.Most times, you simply use the character literal, and dont worry about the Unicode number.

13Character Type ExampleNew Topics:char typeCharacter literalUnicode

14Wrap-Up on LiteralsInteger LiteralsSimply the number itselfExample: 5Assumes literal is type int, so if using a number outside the range of int, you must add the L suffixExample: 400000000000000LFloating-Point LiteralsSimply the number itselfExample: 5.8Assumes literal is type double, so if you want it to be of type float, you must add the F suffixExample: 5.8FYou can also use E Notation2.75649E4 = 2.75649 X 104 = 27564.9

15Wrap-Up on LiteralsBoolean LiteralsReally are none, but there are two predefined boolean valuestrue or falseCharacter LiteralsA single character surrounded in single quotes'G'String LiteralsA series of characters surrounded in double quotes"Banana!"Cannot use most operators on entities of different typeExample: 3 + 4.5 is invalid!

16Variables NotesVariables only hold one value at a time:int number;number = 1;System.out.println(number);number = 2;System.out.println(number);This will print12After these statements, number will hold only the value of 2 and not have any data kept about the value 1.17Variable NotesYou can declare multiple variables of the same type on a single line by separating them with a ,.int a;int b;int c;Is the same as:int a, b, c;18Variable NotesYou can also assign a variable a value on the same line as you declare it:int a;a = 2;Is the same as:int a = 2;This is called initialization.19Arithmetic OperatorsJava offer many operators for manipulating data.Three types of operators:unary takes one operandbinar y takes two operandsternary takes three operandsOperands are the elements that the operator performs an operation on.Example: 2 + 3+ is the operator.2 and 3 are the operands.+ in this case is a binary operator because it takes two operands.Arithmetic Operators are operators that perform basic arithmetic on the numeric data types.20Arithmetic OperatorsOperatorMeaningTypeExample+AdditionBinarya = b + c;-SubtractionBinarya = b - c;*MultiplicationBinarya = b * c;/DivisionBinarya = b / c;%ModulusBinarya = b % c;-NegationUnarya = -b;21Arithmetic Operator NotesModulus returns the remainder of the division of the left operand by the right operand.Example 7 % 5 results in 2The operands of arithmetic operators can be literals or variables.All of these operators work for both integers and floating-point data types, but division of integers is different than floating-point numbers.Integer division divides the left operand by the right operand and discards the remainder if there is one.22Arithmetic Operator ExampleNew Topics:Arithmetic Operators23Operator Precedence and GroupingOperators have precedence just like they do in normal arithmetic:negation has highest precedencemultiplication and division have next (left to right)addition and subtraction have last (left to right)Example: -a + b / ca would be negated firstb / c would happen next-a + the result of b / c would happen lastAlso, like in normal arithmetic, you can group parts of a mathematical expression using parenthesesExample: (-a + b) / ca would be negated first-a + b would happen nextThe result of -a + b would then be divided by c

24The Math ClassThe Java API provides a class called Math.This class provided methods to perform more advanced math operations.Example: Square Roota = Math.sqrt(9.0)a would receive the value of 3.Example: Exponentsa = Math.pow(4.0, 2.0)a would receive the value of 16.

25Grouping and Math Class ExampleNew Topics:Operator PrecedenceGroupingMath Classsqrt method

26