Professional Documents
Culture Documents
INTRODUCTION TO JAVA
WELCOME
History First Java Application Data types Variables Strings Assignments Math, Boolean expressions Relational operations If statements System.exit
Started in 1991 by SUN Microsystems Targeted at consumer electronics. Wanted reliable programming language. Integrated into browsers Evolved into write once run anywhere, integrates into Netscape General purpose libraries released
COURSE INFORMATION
BASIC DEFINITIONS
FIRST APPLICATION
/** *Hello World, first application, only output. */ import java.io.*; public class hello{ public static void main (String [] args) { System.out.println(Hello World\n); } //end main }//end class
Why?
javac hello.java java hello
To compile:
To run:
NOTICE:
Java is CASE SENSITIVE!! Whitespace is ignored by compiler Whitespace makes things easier to readhence it affects your grade File name has to be the same as class name in file. Need to import necessary class definitions
VARIABLES
Variables:
Naming:
11
ASSIGNMENT
12
STRINGS
Not a primitive class, its actually something called a wrapper class To find a built in classs method use API documentation. String is a group of chars A character has single quotes char c = h; A String has double quotes String s = Hello World; Method length int n = s.length;
13
USING STRINGS
public class hello{ public static void main (String [] args) { String s = Hello World\n; System.out.println(s); //output simple string } //end main }//end class hello
14
MATH
Unary
int x = -9;
int y = 3+x;
% modulo operator
15
INCREMENTING
Increment and Decrement i++ equivalent to i = i + 1; Can also do ++i, which uses i before incrementing it. Decrementing: i--;
16
CASTING
int n = 40; Wrong : byte b = n; why?? Right: byte b = (byte) n; Type casting converts to target type
17
CASTING II
Type char is stored as a number. The ASCII value of the character. A declaration of :
char c = B; stores the value 66 in location c can use its value by casting to int how??
18
ASSIGNMENT
+= -= *= /= %=
19
BOOLEAN EXPRESSIONS
boolean b b will be either true (1) or false (0) Logical operations: !(not), && (and) || (or) boolean a,b; a = true; b = false; System.out.println (a && b is + (a && b));
20
RELATIONAL OPERATORS
==
!=
> <
>=
<=
equality inequality greater than less than greater than or equal to less than or equal to
21
if ( x < y) {
x = y; }
22
IF/ELSE
if (logic condition) { something } else if (logic condition) { something } else { something else }
23
NESTED IF
if ( x < 0 ) { System.out.println( x is negative ); } else { if ( x > 0 ) { System.out.println ( x is positive ); } //end if x > 0 else { System.out.println ( x is zero ); } } //end else x >=0
TOPS Technologies Java Introductory tutorial M1
24
SWITCH/CASE
Switch(variable) {
case(1): something; break; case(23): something; break; default: something; }
25
EXCEPTIONS
Java exception object. java.io.Exception most general one. Some exception like in Throwable class define methods to get the message.
26
27
SYSTEM.OUT.PRINTLN
public void println(String x) can be any type of string or combination string using addition to join parts. Example: println(hello + world + x);
28
SYSTEM.EXIT()
One method in java.lang.System Defined: public static void exit ( int status) Terminates currently running Java VM Status is status code, non zero will usually mean something abnormal. Used at end to indicate success, or in middle to signal problems.
29