Professional Documents
Culture Documents
System.o:t.5rint( Wecome )
System.o:t.5rintn( to Java! )
} // end of main()
} // end of cass Wecome1
This will still produce the same text as the
previous version.
,;,Copyright 2000-2004 1om lunter
public class Welcome1
,
public static void main( String args, )
,
System.out.print( Welcome\nto\nJava! );
, // end of main()
, // end of class Welcome1
Notice the ' \n . The slash is an escape
character. It tells the System object that whatever
Iollows the slash is special:
\n new line
\t tab
\r carriage return
\\ backslash
\ quote
Welcome
to
Java!
,;,Copyright 2000-2004 1om lunter
import javax.swing.JJptionPane
public class Welcome4
,
public static void main( String args, )
,
JJptionPane.showMessageDialog( null, Hi Java! );
System.exit( 0 )
, // end of main()
, // end of class Welcome1
First GUI: JOptionPane
This adds an import statement, which tells the
compiler you want to use somebody else`s class.
The ' javax.swing is like a DOS path.
,;,Copyright 2000-2004 1om lunter
import javax.swing.JJptionPane
You must know these classes, and how to use them.
This path helps the compiler Iind the class you wish to
use.
The javax.swing portion oI this name is called the
'package.
Classes in the same package have a connection we will
explore later.
SuIIice it to say that they are very chummy.
First GUI: JOptionPane
,;,Copyright 2000-2004 1om lunter
First GUI: JOptionPane
import javax.swing.JJptionPane
public class Welcome4
,
public static void main( String args, )
,
JJptionPane.showMessageDialog( null, Hi Java! );
System.exit( 0 )
, // end of main()
, // end of class Welcome1
The Statement JO5tionPane.showMessageDiaog means:
'I want object JOptionPane to perIorm its method
showMessageDiaog(). Also, I`m passing the data:
'null and 'Hi Java! In Java, we call that data
'arguments.
,;,Copyright 2000-2004 1om lunter
import javax.swing.JJptionPane
public class Welcome4
,
public static void main( String args, )
,
JJptionPane.showMessageDialog( null,
Hi Java! )
System.exit( 0 )
, // end of main()
, // end of class Welcome4
System.exit( 0 ) This statement uses the
method 'exit oI class System to end the application.
GUI Applications always require this statement to
terminate correctly.
Class System is imported automatically, in package
java.ang
,;,Copyright 2000-2004 1om lunter
Build An Application
When you are building an Application, there is a set
template Ior design that you automatically Iollow.
Get in the habit oI doing exactly as will be done on the
next Iew slides.
Addition
,;,Copyright 2000-2004 1om lunter
import javax.swing.JJptionPane;
1.) You tell the compiler to import any oI the extra
classes you will be using in your Application.
,;,Copyright 2000-2004 1om lunter
import javax.swing.JJptionPane;
public class Addition
statement1;
statement2;
} //end of if else
statement3;
statement4; } //
end of else
Don`t bother to label the
closing brackets unless
you have a really long iI.
Still you should always line
up your brackets.
,;,Copyright 2000-2004 1om lunter
'if Statement Syntax
if( expression )
statement;
else if( expression )
statement;
else
statement;
Compact if/ese if/ ese
To prevent your nested iI`s Irom marching across
the page, you can use this nested iI. You can go on nesting
them as long as you like, and the last one is just an else.