Download presentation
Presentation is loading. Please wait.
Published byHadi Setiabudi Modified over 5 years ago
1
© A+ Computer Science - www.apluscompsci.com
Basic Java © A+ Computer Science -
2
© A+ Computer Science - www.apluscompsci.com
A Simple Class public class AplusCompSci { } This is a very simple Java class. The name of the class is AplusCompSci. All Java programs start with a class. Pieces are added to the class to make a complete program. All Java programs start with a class. © A+ Computer Science -
3
© A+ Computer Science - www.apluscompsci.com
A Simple Class + main public class AplusCompSci { public static void main(String[] args) System.out.println("Aplus Comp Sci!"); } This AplusCompSci class is a bit more sophisticated than the previous one. This AplusCompSci class has a single method named main. The main method is typically used to test the class in which it is contained. For this particular example, the main method contains a statement that prints out Aplus Comp Sci! OUTPUT Aplus Comp Sci! © A+ Computer Science -
4
© A+ Computer Science - www.apluscompsci.com
Syntax Rules public class AplusCompSci { //open brace public static void main(String[] args) { System.out.println("Aplus Comp Sci!"); } } //close brace Java requires that all classes and methods have an open brace { and a close brace }. Braces come in pairs; thus, every open { brace must have a matching close } brace. Braces are used to indicate the beginning of a code block and the ending of a code block. Program statements are placed inside of the code blocks starting after the open brace and ending before the close brace. Braces – You gotta have ‘em! Every class and every method must have a { and a } . © A+ Computer Science -
5
© A+ Computer Science - www.apluscompsci.com
Syntax Rules public class AplusCompSci { public static void main(String[] args) System.out.println("Aplus Comp Sci!"); } In Java, all program statements are terminated with a semi-colon ; . System.out.println() is a program statement and must be terminated with a ; . public class CompSci is a class declaration not a program statement; as a result, there is no terminating ; . You must put a semi-colon at the end of all Java program statements ( ; ). © A+ Computer Science -
6
© A+ Computer Science - www.apluscompsci.com
Syntax Rules Never put a ; before an open { brace ;{ //illegal }; //legal The rule above simply states that you should never place a semi-colon before an open brace { . Following this rule will cut down on syntax errors. © A+ Computer Science -
7
© A+ Computer Science - www.apluscompsci.com
Indentation public class AplusCompSci { public static void main(String[] args) System.out.println("Aplus Comp Sci!"); } Indent all code 3 spaces to make it easier to read. Indentation and spacing is not required. Java will allow entire programs to be written on a single line, but this style is strongly discouraged. Indenting code statements 3 spaces is a good style to indicate that the statements are inside of a particular block of code. System.out.println() is inside of method main; thus, it is indented 3 spaces to make this visibly clear. The main method is inside of class CompSci which is why it is indented 3 spaces. 123 © A+ Computer Science -
8
© A+ Computer Science - www.apluscompsci.com
apluscompsci.java © A+ Computer Science -
9
© A+ Computer Science - www.apluscompsci.com
Basic Java Output © A+ Computer Science -
10
frequently used methods © A+ Computer Science - www.apluscompsci.com
System.out frequently used methods Name Use print(x) print x and stay on the current line println(x) print x and move to next line down printf(s,x) print x according to s specifications The chart above lists the most commonly used System.out methods. This chart is a great reference when preparing for quizzes and tests and when working on lab assignments. © A+ Computer Science -
11
© A+ Computer Science - www.apluscompsci.com
Basic Java Output reference command / method System.out.print("aplus compsci"); OUTPUT aplus compsci System is a class that contains a reference named out. out is a static reference to a PrintStream. out can be used via methods print, println, and printf to display values on the console window. © A+ Computer Science -
12
© A+ Computer Science - www.apluscompsci.com
Basic Java Output System.out.print("aplus compsci"); OUTPUT aplus compsciaplus compsci print is a method used to print values on the console window. print will print a value and remain on the same line as the value printed. © A+ Computer Science -
13
© A+ Computer Science - www.apluscompsci.com
Basic Java Output System.out.println("aplus compsci"); OUTPUT aplus compsci println is a method used to print values on the console window. println will print a value on the current output line and then move down to the next line. © A+ Computer Science -
14
© A+ Computer Science - www.apluscompsci.com
Basic Java Output System.out.println("aplus compsci"); OUTPUT aplus compsci aplus compsci println is a method used to print values on the console window. println will print a value on the current output line and then move down to the next line. This examples shows that aplus compsci is printed on the first line and then aplus compsci is printed on the second line. This output occurs because both output commands use println. © A+ Computer Science -
15
© A+ Computer Science - www.apluscompsci.com
one.java © A+ Computer Science -
16
Intermediate Java Output © A+ Computer Science - www.apluscompsci.com
Escape Sequences \n newline \t tab \r carriage return \b backspace System.out.println("aplusc\tompsci"); \n, \t, \r, and \b are common escape sequences used with print, println, and printf . \t is used to tab over five spaces. \n is used to move the cursor down to the next line. \r is used to move the cursor to the beginning of the current output line. \b is used to backspace one place on the current line. OUTPUT aplusc ompsci © A+ Computer Science -
17
Intermediate Java Output © A+ Computer Science - www.apluscompsci.com
Escape Sequences \n newline \t tab \r carriage return \b backspace System.out.println("apluscom\tpsci"); \n, \t, \r, and \b are common escape sequences used with print, println, and printf . \t is used to tab over five spaces. \n is used to move the cursor down to the next line. \r is used to move the cursor to the beginning of the current output line. \b is used to backspace one place on the current line. OUTPUT apluscom psci © A+ Computer Science -
18
Intermediate Java Output © A+ Computer Science - www.apluscompsci.com
Escape Sequences \n newline \t tab \r carriage return \b backspace System.out.println("apluscomp\nsci"); \n, \t, \r, and \b are common escape sequences used with print, println, and printf . \t is used to tab over five spaces. \n is used to move the cursor down to the next line. \r is used to move the cursor to the beginning of the current output line. \b is used to backspace one place on the current line. OUTPUT apluscomp sci © A+ Computer Science -
19
© A+ Computer Science - www.apluscompsci.com
two.java © A+ Computer Science -
20
Intermediate Java Output © A+ Computer Science - www.apluscompsci.com
Escape Sequences \\ outs \ \" outs " \’ outs ’ System.out.println("aplus\\compsci\"/"); \\, \", and \' are common escape sequences used with to print out a \, ', and a ". \\ is used to print out a single \. \" is used to print out a single ". \' is used to print out a single '. OUTPUT aplus\compsci"/ © A+ Computer Science -
21
Intermediate Java Output © A+ Computer Science - www.apluscompsci.com
Escape Sequences \\ outs \ \" outs " \’ outs ’ System.out.println("aplus\\'comp\'sci\'/"); \\, \", and \' are common escape sequences used with to print out a \, ', and a ". \\ is used to print out a single \. \" is used to print out a single ". \' is used to print out a single '. OUTPUT aplus\'comp'sci'/ © A+ Computer Science -
22
Escape Sequences frequently used combinations Name Use \t
tabs over five spaces \n moves to front of next line \b deletes previous character \r moves to front of current line \\ nets one backslash \ \" nets one double quote " \’ nets one single quote ’ This chart lists the most commonly used escape sequences. This chart should be a great reference point when working on labs and when preparing for quizzes and tests. © A+ Computer Science -
23
© A+ Computer Science - www.apluscompsci.com
Basic Java Comments // single-line comments /* */ block comments //this line prints stuff on the screen System.out.println("aplus cs"); Comments are used to add clarity and descriptions to code. When properly placed, comments can add quite a bit of readability to a piece of code. // is a single line comment used for a single line. /* */ is used when multiple comment lines are needed. Comments are also very useful to isolate a section of code when testing/debugging. It is very handy to comment off a section of code in order to the test the remaining code. © A+ Computer Science -
24
© A+ Computer Science - www.apluscompsci.com
Basic Java Comments // single-line comments /* */ block comments /* this line prints stuff on the screen */ System.out.println("aplus cs"); Comments are used to add clarity and descriptions to code. When properly placed, comments can add quite a bit of readability to a piece of code. // is a single line comment used for a single line. /* */ is used when multiple comment lines are needed. Comments are also very useful to isolate a section of code when testing/debugging. It is very handy to comment off a section of code in order to the test the remaining code. © A+ Computer Science -
25
Intermediate Java Output © A+ Computer Science - www.apluscompsci.com
System.out.printf("%s","apluscs\n"); OUTPUT apluscs printf is a method used to print values on the console window. printf can be used to set the number of decimal when printing a decimal number. printf can be used to print any type of data. © A+ Computer Science -
26
© A+ Computer Science - www.apluscompsci.com
three.java © A+ Computer Science -
27
© A+ Computer Science - www.apluscompsci.com
Advanced Java Output System.out.println( ); OUTPUT 24 This example just adds up the numbers and prints out the sum which is 24. © A+ Computer Science -
28
© A+ Computer Science - www.apluscompsci.com
Advanced Java Output System.out.println( 7 + " " ); OUTPUT 7 89 This one prints 7 and then encounters quotes with a space in between. Double quotes indicate a string. Anything encountered after a string is converted to a string. This code would print 7 89. © A+ Computer Science -
29
© A+ Computer Science - www.apluscompsci.com
Advanced Java Output System.out.println( " " + 9 ); OUTPUT 15 9 This example is similar to the prior 2 examples. 7 and 8 are added together before the double quotes are encountered. 15 is output first. 9 is added to the 15 and space but not added via math as the double quote string in encountered first. © A+ Computer Science -
30
© A+ Computer Science - www.apluscompsci.com
four.java © A+ Computer Science -
31
© A+ Computer Science - www.apluscompsci.com
Programming Errors Syntax errors occur when you type something in wrong, causing the code to not compile. //missing semicolon - ; expected System.out.println("aplus cs") //case problem – should be System system.out.println("aplus cs") © A+ Computer Science -
32
© A+ Computer Science - www.apluscompsci.com
Programming Errors Runtime errors occur when something goes wrong while the program is running. //an out of bounds exception is thrown String s = "runtime_error"; System.out.println( s.charAt(15) ); © A+ Computer Science -
33
© A+ Computer Science - www.apluscompsci.com
five.java © A+ Computer Science -
34
Work on Programs! Crank Some Code!
© A+ Computer Science -
35
© A+ Computer Science - www.apluscompsci.com
Basic Java © A+ Computer Science -
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.