0% found this document useful (0 votes)
30 views

JAVA®Programming 1

The document discusses the Java programming environment including that a Java program consists of classes, each class is saved in its own file and contains a main method. It provides a code template and examples of basic Java programs that output text and perform simple math operations.

Uploaded by

MaxWu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

JAVA®Programming 1

The document discusses the Java programming environment including that a Java program consists of classes, each class is saved in its own file and contains a main method. It provides a code template and examples of basic Java programs that output text and perform simple math operations.

Uploaded by

MaxWu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

JAVA

Programming
I
JAVA

Environment
*a JAVA

program consists of one


or more classes

*each class has a name and is
saved in a separate file with
the same name as the class and
the suffix .java
*every JAVA

application contains
a subprogram called main

*there can also be other subprograms which
are generally referred to as methods

*it is convention is to have class names
begin with an upper case letter, and
object and method names begin with a
lower case letter

JAVA

Template
// The "X" class.
import java.awt.*;
import hsa.Console;

public class X
{
static Console c; // The output console

public static void main (String[] args)
{
c = new Console();

// Place your program here. 'c' is the output console
} // main method
} // X class
Comments
*there are two sorts of comments in JAVA


/* This is a comment. It does not end here...
but keeps on going until it reaches a */

// This is also a comment which ends at the end of the line

*every program should have a header with the
following information

//name:
//date:
//description:

*include comments throughout the program to indicate
what is happening


Example Programs
1. c.print("Hello world!");
output: Hello world!

2. c.print("a");
c.print("b");
output: ab

3. c.println("a");
c.println("b");
output: a
b

4. c.println();
output:
[nothing]

Example Programs
4. c.println(1 + 2);
output: 3

5. c.println(1 + 2 + " is three");
output: 3 is three

6. c.print(1/3);
output: 0

7. c.print(1.0/3.0);
output: 0.3333333333333333
[16 fractional digits]

8. c.print(Math.sqrt(2));
output: 1.4142135623730951
[16 fractional digits]

You might also like