Topic 2: Introduction to Java and Dr Java
Dr Java is an integrated development environment (IDE) for Java programming
o Writing the code, compiling, running, debugging can be done in this one program
Dr Java has several panes
o Definition pane: Used for creating (tying in and editing) complete java programs
o Interaction pane: Used for testing of specific code without having to compile; unique to Dr Java
o Files pane: All currently open and used classes are listed here.
To try out Dr Java, we will begin with simple math: evaluating arithmetic expressions
Arithmetic expressions consists of operands (values) and operators and represent a numeric value
o Addition: +
o Subtraction: -
o Multiplication: *
o Division: /
o Negation: –
o Modulo: % (Remainder after integer division. Returns whole numbers without any decimal points)
Default evaluation order:
o Parentheses ()
o Negation –
o Multiplication, division, and modulo from left to right
o Addition, and subtraction, from left to right
o Add parentheses even in unnecessary situations to make it clear what is happening
o Think like the computer! The computer cannot reason or think and jump ahead.
Notion of Type
Java is a strongly-typed language
Every value has a „type‟ associated with it, which tells how the computer to interpret values.
Primitive Data Type
Used to store simple data values, such as integers, floating points, characters, and Booleans.
Used for efficiency reasons, as they take very little memory, and allow fast computation.
Each type stores a specific range of numbers within a specific amount of memory.
o Stores whole numbers only. „Int‟ is used most often.
o Byte: Uses 8 bits. Holds range of numbers between -100 to 100.
o Short: Uses 16 bits. Holds range of numbers between -32,000 to 32,000. o Int: Uses 32 bits. Holds range of numbers between -2.1 billion to 2.1 billion.
o Long: Uses 64 bits.
Floating Point Values:
o Stores all real values in scientific notation. „Double‟ is used most often.
o Float: Uses 32 bits.
o Double: Uses 64 bits.
o Changes the type of one of the integer type value to a floating point value type (or vice versa)
o More often used when there are variables holding values, instead of actual numbers.
Ex. Casting 3 in (3 / 2) to a double: (double) 3 / 2
o Not always necessary. Adding a .0 at the end of a number can have the same effect.
o Stores a single value of any alphabets, numbers, or symbols using Unicode encoding.
o Char: Uses 16 bits
o Compares two operands of the same type with relational operators.
o Holds only „true‟ or „false‟.
o Relational Operators:
Greater than: >
Less than: <
Not equal: !=
Greater than or equal: >=
Less than or equal: <=
String: Object variable-type for any length of a sequence of characters
o Strings represent all text data
o Objects are not limited in size, so strings can have as many characters as wished.
o Java compiler recognizes strings as beginning and ending with a double quotation
Null string: A string with no values, and is represented by double quotes with nothing in between “”
Concatenation: Java can add (or concatenate) multiple strings using the concatenation operator: +
o The result of this is a new string with the characters of second strings written after characters of the first
Ex. System.out.println(“CS1026” + “a” + “/” + “b”) = CS1026a/b
o Numbers can also be concatenated with strings changing the numbers into strings, then concatenating them
Ex. System.out.println(“The total is” + 10) = The total is 10
Escape Character: Special character (\) that is used to allow special characters to be embedded into a string
o Add a double quotation : \”
o Add a \: \\ o Add a tab: \t
o Move to the next line: \n
Java programs are made up of unique statements for programming
Java statements end in a semicolon. Having missing semicolons lead to syntax errors.
Ex. Assignment Statement:
o Assignment statements are used to assign a variable a new value
o =; to assign the value of a variable to what comes after the =
Ex. Print-out Statement:
o Print statements are used when a value needs to be outputted.
o System.out.println(); to print the value of the expression between the parenthesis, then move to a new line
o System.out.print(); to print the value of the expression between the parenthesis without moving to a new line
Variables: Locations in memory containing a value, and labeled with a name
o Called „variable‟ because the content can vary, depending on what the programmer stores or calculates
o Variables can be created and named by declaring, an