{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec4 - 1.00 Lecture 4 Data Types Operators Reading for next...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1.00 Lecture 4 Data Types, Operators Reading for next time: Big Java: sections 6.1-6.4 (still) Promotion Data Type Allowed Promotions double None float double long float,double int long,float,double char int,long,float,double short int,long,float,double byte short,int,long,float,double increasing capacity Java does promotions silently, from lower capacity types to higher capacity types in operations and assignment (=) When doing binary operations, Java promotes byte or short to int In all other cases it promotes the smaller to larger capacity Don’t mess around: just use int (long sometimes) and double
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Casting To convert a data type to a lower capacity type, you must cast it explicitly long s= 1000*1000*1000; int q= (int) s; The programmer is responsible for making sure the variable fits in the lower capacity representation. If it doesn’t, you get no warning, and there is garbage in the variable (more next class on this topic) You can cast variables into higher capacity types, if needed You already did this, casting ints to doubles Exercise Create a new project (Lecture4) Write a class CastTest. In the main() method: Declare int s x1=17 , x2=20 and x3=12 Try to declare an int 2x= 34 . What happens?
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}