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 programming, focusing on primitive types and reference types. Primitive types include byte, short, int, long, float, double, char, and boolean. The limitations of primitive types, how to perform operations on them, and introduces reference types, objects, and classes. It also covers the concept of instantiation, object variables, and method invocation.

Typology: Assignments

Pre 2010

Uploaded on 08/31/2009

koofers-user-oc2
koofers-user-oc2 🇺🇸

3

(2)

10 documents

1 / 21

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 CS162: Introduction to Computer Science II Java Fundamentals 1 Primitive Types 2 2 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 t i d t l f th d l d t 3 con a ns a a a va ue o e ec are ype. The eight primitive types in Java 4 5 Short-circuit evaluation int x = 3; i t 5n y = ; int z = x / y; if ((z != 0) && ((y / z) < 5)) // OK 9 if (((y / z) < 5) && (z != 0)) // crash if DivideByZeroException // is not handled 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) th l ti f th d t 10 e memory oca on o e a a. 6 Reference Types 11 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 12 7 Classes • A class defines the characteristics for all objects of its type • A class gives a general description of – what an object of the type is (instance data) – what an object of the type can do (methods) 13 Objects / Classes • An object is an instance of a class. A program ma ha e man instances• y v y (objects) of one class. • All instances of the same class have – The same kinds of data – The same methods 14 10 Instantiation bobsCar “Sedan” 2000 0.9 55 21405 A memory cell that contains the memory address of the data for 19 bobsCar Aliases • It is possible for two variables to reference the same object. They are aliases • Example: Automobile myCar = bobsCar; bobsCar “Sedan” 2000 0 9 20 myCar . 55 21405 If an object has two (or more) aliases, the object may be referenced and/or modified through any of those aliases. 11 Invoking methods in a program • Instead of calling a function to “do something to” an object (imperative programming), tell the object to perform one of its actions (object- based programming). • Examples: bobsCar.accelerate(5); int currentSpeed = bobsCar.getSpeed(); 21 if (bobsCar.getFuelLevel() < 0.1) bobsCar.decelerate(10); Invoking methods in a program • Valued methods return a single value, and should be used in an expression. – Return type of method must be assignment- compatible in the context of the call • void methods do not return a value, and should be used as a single statement. E l 22 • xamp es: bobsCar.accelerate(5); int currentSpeed = bobsCar.getSpeed(); if (bobsCar.getFuelLevel() < 0.1) bobsCar.decelerate(10); 12 Arguments and Parameters • Invocation of a method must have same b f t f l t inum er o argumen s as orma parame ers n the declaration of the method. • Arguments are associated in order with formal parameters • Argument types must be assignment- 23 compatible with the associated formal parameter types Arguments and Parameters • The formal parameter in the method becomes an alias for the argument passed to it, and is treated as a local variable. • The formal parameter is discarded when the method terminates. • Example: Suppose that a matchSpeed method is defined as follows: 24 public void matchSpeed(Automobile other) { int diff = getSpeed() - other.getSpeed(); if(diff > 0) other.accelerate(diff); else other.decelerate(-diff); } 15 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 29 passed to a method! – An object referenced by an argument can be changed by a method. Alternate implementation of matchSpeed * public void matchSpeed(Automobile other) { otherCar = new Automobile(other.getModel(), other.getYear(), other.getFuelLevel(), getSpeed(), other.getMileage()); 30 } * WRONG! 16 Before calling matchSpeed * bobsCar “Sedan” 2000 suesCar “SUV” 2001 31 0.9 55 21405 0.45 35 9864 Call matchSpeed * bobsCar.matchSpeed(suesCar); suesCar is associated with other bobsCar “Sedan” 2000 suesCar “SUV” 2001 other is a parameter, an alias of suesCar otherCar is a local variable with the same instance values as other 32 0.9 55 21405 other 0.45 35 9864 17 After executing the statement other = new Automobile( … ); other refers to a new suesCar “SUV” 2001 bobsCar “Sedan” otherCar “SUV” memory location other 0.45 35 9864 33 2000 0.9 55 21405 2001 0.45 55 9864 After matchSpeed * terminates suesCar “SUV” 2001 other no longer exists otherCar and the object it f d l i t otherCar “SUV”bobsCar “Sedan” other 0.45 35 9864 re erence no onger ex s suesCar.speed is unchanged Memory allocated by new is garbage 34 2001 0.45 55 9864 2000 0.9 55 21405 20 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 39 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) 40 int x = i; // auto-unboxing // same as int x = i.intValue(); 21 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 41 3. Auto-box the result into a new Double 4. Store a reference to the newly created wrapper object in e A Note About Wrappers • Storing wrapper numbers is quite inefficient This is beca se o often onl ant to store• u y u y w 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 42
Docsity logo



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