





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of java programming basics in the context of the cmsc 131 spring 2009 course. Topics covered include java's portability, java virtual machine (jvm), method headers, and output. Learn about java's unique approach to portable object code, the role of the jvm in running java bytecode, and the importance of method headers in defining operations and their operands and results.
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
1
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
2
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
3
Java Virtual Machine
.java
Java compiler
.class
client
client
source code object code
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
4
Facts about JVMs
For efficiency, JVMs often compile bytecode
into native machine code
There are also “native” Java compilers (these
compile Java directly to machine code)
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
5
Method Headers
Operand is collection of Strings
Result is “void” (= unimportant)
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
9
Holding and calculating values
variables
mathematical expressions
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
10
Java Program Organization
public static void main( String[ ] args ) { … (contents of the main method go here) … }
int secondsPerMinute = 60; int minutesPerLecture = 50;
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
11
Java Program Organization
Statements: Many different types
int x, y, z; // three integer variables String s = “Howdy”; // a character string variable boolean isValid = true; // a boolean (true/false) variable
x = 13;
System.out.println( “Print this message“ );
Built-in Operators: For manipulating values (+, -, *, /, etc.)
i.e. String Concatenation for output
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
12
Built-in (Primitive) Types
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
13
String Type
Elements of String type are sequences of
characters
“abc” “Call me Ishmael” etc.
String type is not built-in
We will use it a lot
Useful operation:
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
14
Writing Programs in Java
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
18
Primitive-Type
Literals/Constants
Constants are also called literals
Integer types: byte short optional sign and digits (0-9): 12 -1 +234 0 1234567 int long Same as above, but followed by ‘L’ or ‘l’: -1394382953L
Floating-point types : double Two allowable forms:
Decimal notation : 3.14159 -234.421 0.0042 -43.
Scientific notation : (use E or e for base 10 exponent) 3.145E5 = 3.145 x 10^5 = 314500.
1834.23e-6 = 1834.23 x 10-6^ = 0. float Same as double, but followed by ‘f’ or ‘F’: 3.14159F -43.2f
Note : By default, integer constants are int , unless ‘L’/‘l’ is used to indicate they are long. Floating constants are double , unless ‘F’/‘f’ is used to indicate they are float.
Avoid this lowercase L. It looks too much like the digit ‘1’
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
19
Character and String
Constants
Char constants : Single character in single quotes (‘…’) including: Letters and digits : ‘A’, ‘B’, ‘C’, …, ‘a’, ‘b’, ‘c’, …, ‘0’, ‘1’, …, ‘9’ Punctuation symbols : ‘*’, ‘#’, ‘@’, ‘$’ (except ‘ and backslash ‘\’) Escape sequences : (see below) String constants : 0 or more characters in double quotes (“…”) Escape sequences : Allows inclusion of special characters: \” double quote \n new-line character (start a new line) \’ single quote \t tab character \ backslash Examples : char x = ’\’’; → (x contains a single quote) String s1= ”\”Hi there!\””; → s1 contains ”Hi there!” String s2= ”C:\WINDOWS”; → s2 contains C:\WINDOWS
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
20
Common Numeric Operators
Arithmetic operators : Unary negation: -x Addition/subtraction: x+y x-y
Multiplication/division: x*y x/y Division between integer types truncates to integer: 23/4 → 5 x%y returns the remainder of x divided by y: 23%4 → 3 Division with real types yields a real result: 23.0/4.0 → 5.
Comparison operators : Equality/inequality: x == y x != y
Less than/greater than: x < y x > y Less than or equal/greater than or equal: x <= y x >= y
These comparison operators return a boolean value: true or false.
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
21
Common String Operators
String Concatenation : The ‘+’ operator concatenates (joins) two strings. “Go” + “Terps” →→→→ “GoTerps”
When a string is concatenated with another type, the other type is first evaluated and converted into its string representation.
(84) + “degrees”* →→→→ “32degrees” (1 + 2) + “ 5 ” →→→→ “ 35 ”
String Comparison : Strings have special comparison functions. s.equals(t) : returns true if s and t have the same characters. s.compareTo(t) : compares strings lexicographically (dictionary order) result < 0 if s precedes t result == 0 if s is equal to t result > 0 if s follows t
“dilbert”.compareTo( “dogbert” ) →→→→ -1 (which is < 0)
Note: Concatenation does not add any space
Both functions are case-sensitive.
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
22
User Input in Java
We've done output (System.out); what about
input?
Java 5.0 includes the Scanner class feature
To use Scannner need to import a library:
import java.util.Scanner;
CMSC 131 Spring 2009 Jan Plane (adapted from Bonnie Dorr)
23
Scanner Class Details
To create a scanner object: new Scanner( input_source ); Input source can be keyboard (System.in), files, etc. Object must be assigned to a variable (e.g. sc) Operations nextBoolean() nextByte() nextDouble() nextFloat() nextInt() nextLong() nextShort()
next() Returns sequence of characters up to next whitespace (space, carriage return, tab, etc.)
nextLine() Returns sequence of characters up to next carriage return
Returns value of indicated type (reports error if type mismatch