introduction to programming in java
DESCRIPTION
Introduction to Programming in Java. AP Computer Science A Dr. Persin. Contents. History First Java Application Data types Variables Strings Assignments Math, Boolean expressions Relational operations If statements System.exit. Very Brief History. Started in 1991 by SUN Microsystems - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/1.jpg)
www.Lnk2Lrn.com 1
Introduction to Programming in Java
AP Computer Science A
Dr. Persin
![Page 2: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/2.jpg)
www.Lnk2Lrn.com 2
Contents
• History• First Java Application• Data types• Variables• Strings• Assignments• Math, Boolean expressions• Relational operations• If statements• System.exit
![Page 3: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/3.jpg)
www.Lnk2Lrn.com 3
Very Brief History
• 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
![Page 4: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/4.jpg)
www.Lnk2Lrn.com 4
Basic Definitions
• Java is an object oriented language.– Object
– Method
– Class
– Applications
– Applets
– Native classes
– Threads
– Exceptions
![Page 5: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/5.jpg)
www.Lnk2Lrn.com 5
First Application
/***Hello World, first application, only output.*/
public class hello{ public static void main (String [] args) {
System.out.println(“Hello World”);} //end main
}//end class
![Page 6: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/6.jpg)
www.Lnk2Lrn.com 6
Notice:
• Java is CASE SENSITIVE!!
• Whitespace is ignored by compiler
• Whitespace makes things easier to read…hence it affects your grade
• File name has to be the same as class name in file.
• Need to import necessary class definitions
![Page 7: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/7.jpg)
www.Lnk2Lrn.com 7
Variables
• Variables:– Name– Type– Value
• Naming:– May contain numbers,underscore,dollar sign, or letters– Can not start with number– Can be any length– Reserved keywords– Case sensitive
![Page 8: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/8.jpg)
www.Lnk2Lrn.com 8
Primitive data typesByte 8 -27 27-1
Short 16 -215 215-1
Int 32 -231 231-1
Long 64
Float 32
Double 64
Boolean 1 0 1
Char 16
![Page 9: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/9.jpg)
www.Lnk2Lrn.com 9
Assignment
• =
• Example:
int n;
n = 10;
or
int n = 10; //same
![Page 10: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/10.jpg)
www.Lnk2Lrn.com 10
Strings
• Not a primitive class, its actually something called a wrapper class
• To find a built in class’s method use API documentation.• String is a group of char’s• A character has single quotes
– char c = ‘h’;• A String has double quotes
– String s = “Hello World”;• Method length
– int n = s.length;
![Page 11: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/11.jpg)
www.Lnk2Lrn.com 11
Using Strings
public class hello{
public static void main (String [] args) {
String s = “Hello World”;
System.out.println(s); //output simple string
} //end main
}//end class hello
![Page 12: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/12.jpg)
www.Lnk2Lrn.com 12
Math
• Unaryint x = -9;
• Regular math (+,-,*,/)int y = 3+x;
• % modulo operator
![Page 13: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/13.jpg)
www.Lnk2Lrn.com 13
Incrementing
• Increment and Decrement
• i++ equivalent to i = i + 1;
• Can also do ++i, which uses i before incrementing it.
• Decrementing: i--;
![Page 14: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/14.jpg)
www.Lnk2Lrn.com 14
Casting
int n = 40;
Wrong : byte b = n;
why??
Right: byte b = (byte) n;
Type casting converts to target type
![Page 15: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/15.jpg)
www.Lnk2Lrn.com 15
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 ccan use its value by casting to inthow??
![Page 16: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/16.jpg)
www.Lnk2Lrn.com 16
Assignment
• +=
• -=
• *=
• /=
• %=
![Page 17: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/17.jpg)
www.Lnk2Lrn.com 17
Boolean Expressions
• boolean bb 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));
![Page 18: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/18.jpg)
www.Lnk2Lrn.com 18
Relational Operators
• == equality
• != inequality
• > greater than
• < less than
• >= greater than or equal to
• <= less than or equal to
![Page 19: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/19.jpg)
www.Lnk2Lrn.com 19
The if - branching statement
• if ( x < y) {x = y;
}
• if ( x < y ) {x = y;
}else { x = 88;}
![Page 20: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/20.jpg)
www.Lnk2Lrn.com 20
If/Else
• if (logic condition) {something}else if (logic condition) { something}else {something else}
![Page 21: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/21.jpg)
www.Lnk2Lrn.com 21
Nested IF
if ( x < 0 ) {System.out.println( “ x is negative “ );}
else {if ( x > 0 ) {
System.out.println ( “x is positive” );}//end if x > 0else {System.out.println ( “x is zero “ );}
} //end else x >=0
![Page 22: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/22.jpg)
www.Lnk2Lrn.com 22
Switch/Case
• Switch(variable){case(1): something;
break;
case(23): something;
break;
default: something;
}
![Page 23: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/23.jpg)
www.Lnk2Lrn.com 23
Exceptions
• Java exception object.
• java.io.Exception most general one.Some exception like in Throwable class define methods to get the message.
![Page 24: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/24.jpg)
www.Lnk2Lrn.com 24
try….catch blocks.
• Try {…….} catch ( IOException v) {……….}
![Page 25: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/25.jpg)
www.Lnk2Lrn.com 25
System.out.println
• println is a method in the Printstream class.• Defined:
– public void println(String x)
can be any type of string or combination string using addition to join parts.Example: println(“hello “ + “world “ + x);
![Page 26: Introduction to Programming in Java](https://reader036.vdocument.in/reader036/viewer/2022081421/56813a27550346895da20a52/html5/thumbnails/26.jpg)
www.Lnk2Lrn.com 26
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.• For more practice go to http://www.javabat.com