java basics - Brookwood High School

Download Report

Transcript java basics - Brookwood High School

JAVA BASICS
SYNTAX, ERRORS, AND
DEBUGGING
College Board
Computer Science A Topics Covered




Program Design - Read and understand a
problem's description, purpose, and goals.
Program Implementation - Console output
(System.out.print/println)
Program Analysis - Identify and correct
errors.
Computing Context - Major hardware
components; Language
translators/compilers ; Virtual machines
GCOC – A.P. Computer Science A
Vocabulary & Terms














Computer programming
Machine language
Machine code
Compiler
Source code
Integrated Development Environment (IDE)
Statement
Bytecode
Java virtual machine (JVM)
Class
Object
Reference
Command
Method
GCOC – A.P. Computer Science A
How Java Works!
1.
2.
3.
4.
You create a source document using an established protocol (in
our case, the Java language).
Then your program is run through a source code compiler. The
source code compiler checks for errors and won’t let you compile
until it’s satisfied that everything will run correctly.
The compiler creates a new document,coded into Java bytecode.
Any device capable of running Java will be able to
interpret/translate this file into something it can run. The compiled
bytecode is platform independent.
The Java Virtual Machine (JVM) translates the bytecode into
something the underlying platform understands, and runs your
program.
The next slide shows an illustration of this sequence.
GCOC – A.P. Computer Science A
How Java Works!
GCOC – A.P. Computer Science A
What is Computer Programming?
Before we jump into Java, let’s talk a bit about
exactly what computer programming is.
 Computer programming is the art of creating a
set of instructions, called a computer program,
for a computer.
 Computers have no judgment, so instructions
must be detailed and unambiguous!
Unfortunately, creating complex sets of
unambiguous instructions is not easy. It requires
both training and practice.
GCOC – A.P. Computer Science A
public class BareBones
{
}
// end class BareBones
All Java programs start with a class.
GCOC – A.P. Computer Science A
public class CompSci
{
public static void main( String args [] )
{
System.out.println(“Java Rules!");
}
OUTPUT
}
Java Rules!
Type this program in Dr. Java and run it. Then change String args [] to
String [] args and run the program again. You should notice no change!
GCOC – A.P. Computer Science A
public class CompSci
{
public static void main( String args [] )
{
System.out.println(“Java Rules!");
}
}
Every method and every class must have an
opening ( { ) brace and a closing ( } ) brace.
GCOC – A.P. Computer Science A
public class CompSci
{
public static void main(String args[])
{
System.out.println(“Java Rules!");
}
}
Every program statement is terminated with
a semi-colon ( ; ).
GCOC – A.P. Computer Science A
Never pu
t a;
before an open { brace
;{ //illegal
}; //legal
GCOC – A.P. Computer Science A
public class CompSci
{
public static void main(String args[])
{
System.out.println(“Java Rules!");
}
}
Indent all code 3 spaces to make it easier to read.
GCOC – A.P. Computer Science A
What is a Convention?
In Java, a convention is an accepted
practice, not necessarily a rule.
 Indenting 3 spaces is a Java convention
that you will see in many textbooks. In
reality, the compiler doesn’t really care
about spacing!
 Spacing is for the human readers, not the
computer.

GCOC – A.P. Computer Science A
Conventions For Use In This Class
You should download and print out a copy
of the document Conventions, which is the
next link.
 You must follow the conventions listed on
this document in every program that you
turn in for a grade!
 You will lose points on your programs if
you do not follow these conventions!

GCOC – A.P. Computer Science A
Basic Output Commands
print()
println()
GCOC – A.P. Computer Science A
object / reference
command / method
System.out.print("Hello"
System );
is the class name.out is an object that prints characters on
the console window (black window on the screen).
• println is a method, or behavior, that the System.out object
carries out.
• The characters enclosed in double quotation marks and are called
a string. The string is printed in the console window.
• Each statement in a program ends with a semicolon (;).
OUTPUT
Hello
Try this in the interactions
pane of Dr. Java. You
should get the output to the
left.
GCOC – A.P. Computer Science A
Type the following into java:
public class TestOutputs
{
public static void main(String [] args)
{
System.out.print("Hello");
System.out.print("Hello");
}
}
OUTPUT
HelloHello
GCOC – A.P. Computer Science A
public class TestOutputs
{
public static void main(String [] args)
{
System.out.println("Hello");
System.out.print("Hello");
}
}
OUTPUT
Hello
Hello
GCOC – A.P. Computer Science A
Try the statements below in your TestOutputs program. Be sure
to run each one and not any differences. Pay close attention to
the difference between print and println.
GCOC – A.P. Computer Science A

Java provides a facility for displaying special characters in a string. These special
characters are indicated by placing a backslash (\) in the string. For example, since
double quotation marks are used to indicate the start and end of a string, there is no
obvious way to include quotation marks within a string.
Therefore, the backslash must be used as in the statement:
System.out.println(“You say \”Goodbye,\” and I say \“Hello.\””);
Try this in the TestOutputs program. It should display:
You say "Goodbye," and I say "Hello.“

The double quotation marks that follow the backslashes are displayed
rather than interpreted as ending the string. The combination of symbols is
called an escape character. The idea is that it escapes from the normal
interpretation of characters in a string.
GCOC – A.P. Computer Science A

Another special escape character is the end-of-line
character. This is indicated by \n and can replace the
use of –ln in println. For example:
System.out.print(“This is the first line,\n”);
System.out.print(“and this is the second line.”);
Try this in the TestOutputs program.
Displays:
This is the first line,
and this is the second line.
GCOC – A.P. Computer Science A
\\
\“
\’
\n
\t
Prints out \
Prints out “
Prints outs ’
Goes to next line
Tab(moves over 8 spaces
System.out.println("\\hello\"/");
OUTPUT
\hello"/
GCOC – A.P. Computer Science A
What is the output?
(Attempt to answer the questions first, then type each into the
interactions pane in Dr. Java to confirm your answers).
System.out.println( “h\tello”);
System.out.println( “hel\\lo\””);
System.out.println( “hel\nlo”);
GCOC – A.P. Computer Science A