lec2 - COMP201 Java Programming Topic 2: Java Basics for C+...

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

View Full Document Right Arrow Icon
COMP201 Java Programming Topic 2: Java Basics for C++ Programmers Readings: Chapter 3
Background image of page 1

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

View Full DocumentRight Arrow Icon
COMP201 Topic 2 / Slide 2 Outline Java primitive types Java variables Type conversion among primitive types Operators Control flow Input/Output Arrays Strings
Background image of page 2
COMP201 Topic 2 / Slide 3 First Java Program public class MyProgram { public static void main( String args[]) { System . out .println(“Hello world!”); } } // File : MyProgram.java public : Access modifier class : everything is inside a class MyProgram : class name. matches file name. Case sensitive main : member function of the wrapping class Compilation and run: javac MyProgram.java => MyProgram.class java MyProgram
Background image of page 3

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

View Full DocumentRight Arrow Icon
COMP201 Topic 2 / Slide 4 8 Primitive Types Integers byte (1 byte, -128 to 127) short (2 bytes) int (4 bytes) long (8 bytes) Floating-point types float (4 bytes, 6-7 significant decimal digits) double (8 bytes, 15 significant decimal digits) char (2 byte, Unicode) (ASCII 1 byte) boolean (true or false) Cannot convert between integer s and boolean values. Those are all the primitive types in Java. Everything else is an object. For really large numbers use BigInteger and BigDecimal classes
Background image of page 4
COMP201 Topic 2 / Slide 5 Variables Can be declared anywhere for ( int i = 0; i < 20; i++) { System . out .println(“Hi”); char ch = ‘A’; } double pi = 3.14159; Java compilers require initialization of local variables before use Instance variables of class automatically initialized.
Background image of page 5

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

View Full DocumentRight Arrow Icon
COMP201 Topic 2 / Slide 6 Constants final marks a variable “read-only” Variable is assigned once and cannot be changed Use static final to define constants which are available to multiple methods inside a single class public class Time { static final int MinHour = 0; static final int MaxHour = 23; private int hour, minute; // these properties are set to 0 // unless overwritten by constructor … }
Background image of page 6
COMP201 Topic 2 / Slide 7 Type Conversion Among Primitive Types Type ordering: byte, short, int, long, float, double Need cast for narrowing conversions Double x = 9.997; int num = ( int )x; int num = x; // illegal Widening” conversion is automatic: E.g. assigning short to int , int to float Not okay: boolean b = (boolean) num; Use instead: boolean b = (num != 0); Less general More general
Background image of page 7

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

View Full DocumentRight Arrow Icon
COMP201 Topic 2 / Slide 8 Operators Basically the same as C++: !, ~, ++, --, *, /, %, +, -, =, +=, -=, *=, /=, %=, User cannot “overload” operators; although + is overloaded to do string concatenation Note that methods can be overloaded
Background image of page 8
No pointers! No explicit pointer types (al
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 35

lec2 - COMP201 Java Programming Topic 2: Java Basics for C+...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online