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 Fundamentals: Understanding Primitive Types and Reference Types, Assignments of Computer Science

An introduction to java's primitive types, including byte, short, int, long, float, double, char, and boolean. It covers the limitations of primitive types, operations on primitive types, and the concept of logical operators. Additionally, it introduces reference types, objects, and classes, and explains the concept of instantiating objects and invoking methods.

Typology: Assignments

Pre 2010

Uploaded on 08/31/2009

koofers-user-h12
koofers-user-h12 🇺🇸

10 documents

1 / 7

Toggle sidebar

Related documents


Partial preview of the text

Download Java Fundamentals: Understanding Primitive Types and Reference Types and more Assignments Computer Science in PDF only on Docsity! 1 1 CS162: Introduction to Computer Science II Java Fundamentals 2 Primitive Types 3 Primitive types • Primitive types: byte, short, int, long, float, double, char, boolean • Example: int size = 42; – size is a primitive variable, i.e., a variable that contains a data value of the declared type. 4 The eight primitive types in Java 5 Limitations • All primitive types have limited ranges – Exceeding the range will cause inaccurate results • float and double are typically approximations – don't use == to compare real types – Use Math.abs(x-y) <= EPSILON where EPSILON is some small number 6 Operations on primitive types • Variable holds value • Assignment statements assign values – E.G., int n = 10; • Operations are defined as language primitives – E.G., n += 5; • Relational operators, etc., are defined as language primitives – E.G., if (n <= 15) ... 2 7 Result of logical operators AND OR NOT 8 DeMorgan’s Laws !(x && y) == !x || !y !(x || y) == !x && !y Short-circuit evaluation int x = 3; int y = 5; if ((x == y) && (y > 0)) ... if ((x < y) || (y < 0)) ... 9 Short-circuit evaluation int x = 3; int y = 5; int z = x / y; if ((z != 0) && ((y / z) < 5)) // OK if (((y / z) < 5) && (z != 0)) // crash if DivideByZeroException // is not handled 10 Reference types • All types that are not primitive are reference or class types • Example: String greeting = "Howdy"; – greeting is a reference variable, i.e., a variable that contain a reference to (address of) the memory location of the data. 11 Reference Types 12 Objects • An object is a program entity that – contains data – performs certain actions • The actions are called methods • The actions of various objects interact to form a solution for a given problem 5 25 Before calling matchSpeed bobsCar 21405 55 0.9 2000 “Sedan” suesCar 9864 35 0.45 2001 “SUV” 26 Call matchSpeed bobsCar.matchSpeed(suesCar); bobsCar 21405 55 0.9 2000 “Sedan” other suesCar 9864 35 0.45 2001 “SUV” suesCar is associated with other other is an alias of suesCar. 27 After executing other.accelerate(diff); bobsCar 21405 55 0.9 2000 “Sedan” other suesCar 9864 55 0.45 2001 “SUV” suesCar.speed is changed 28 After matchSpeed terminates bobsCar 21405 55 0.9 2000 “Sedan” other suesCar 9864 55 0.45 2001 “SUV” other no longer exists 29 Passing Arguments • Pass by value – For primitive type, parameter initialized to value of argument in call • Pass by reference – For a class type, formal parameter is initialized to the address of the object in the call • An argument cannot be changed by being passed to a method! – An object referenced by an argument can be changed by a method. 30 Alternate implementation of matchSpeed * public void matchSpeed(Automobile other) { otherCar = new Automobile(other.getModel(), other.getYear(), other.getFuelLevel(), getSpeed(), other.getMileage()); } * WRONG! 6 31 Before calling matchSpeed * bobsCar 21405 55 0.9 2000 “Sedan” suesCar 9864 35 0.45 2001 “SUV” 32 Call matchSpeed * bobsCar.matchSpeed(suesCar); bobsCar 21405 55 0.9 2000 “Sedan” other suesCar 9864 35 0.45 2001 “SUV” suesCar is associated with other other is a parameter, an alias of suesCar otherCar is a local variable with the same instance values as other 33 After executing the statement other = new Automobile( … ); bobsCar 21405 55 0.9 2000 “Sedan” otherCar 9864 55 0.45 2001 “SUV” other refers to a new memory location other suesCar 9864 35 0.45 2001 “SUV” 34 otherCar 9864 55 0.45 2001 “SUV” After matchSpeed * terminates bobsCar 21405 55 0.9 2000 “Sedan” other suesCar 9864 35 0.45 2001 “SUV” other no longer exists otherCar and the object it referenced no longer exist suesCar.speed is unchanged Memory allocated by new is garbage 35 Wrapper Classes 36 Wrapper Classes • Sometimes, you need to treat primitive types like objects • For example, you might want to turn the integer 42 into the string “42” • To do this, you need to: – Create a Wrapper class for the integer – Call the toString() method for that wrapper class 7 37 Wrapper Classes To treat primitive types as objects, you must use wrapper classes Shortshort Longlong Integerint Floatfloat Doubledouble Characterchar Booleanboolean Bytebyte Wrapper ClassPrimitive Class 38 Wrapper Class Example int size = 42; Integer sizeWrapper = new Integer(size); String sizeString = sizeWrapper.toString(); Integer value = 42size = 42 39 Wrapper Class Functions There are lots of other functions you can call on a wrapper class: • doubleValue() – returns the value as a double • intValue() – returns the value as an integer To see the complete list, check out the Java 5.0 API 40 Auto-boxing • In Java 5.0, conversion between primitive types and the corresponding wrapper classes is automatic • This is called auto-boxing Integer i = 42; // auto-boxing // same as Integer i = new Integer(42) int x = i; // auto-unboxing // same as int x = i.intValue(); 41 Fancy Auto-boxing You can even make auto-boxing work inside arithmetic expressions eg. Double e = d + 1; // d is a Double Let’s look at the steps that this involves: 1. Auto-unbox d into a double 2. Add 1 3. Auto-box the result into a new Double 4. Store a reference to the newly created wrapper object in e 42 A Note About Wrappers • Storing wrapper numbers is quite inefficient • This is because you often only want to store the raw value but the wrapper class stores other stuff in addition to the value • If you care about efficiency, use the primitive type where possible
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved