21
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)

Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)

Embed Size (px)

Citation preview

Introduction to Programming

David Goldschmidt, Ph.D.Computer Science

The College of Saint Rose

Java Fundamentals(Comments, Variables,

etc.)

Our First Java Program

Java programs are written in a text editor or IDE and saved with a “.java” filename extension// This is Java

public class Simple

{

public static void main( String[] args )

{

System.out.println( "Hello!" );

}

}

Save this file as Simple.java(names must match)

This is acomment

Curly brackets(a.k.a. braces)must match up

This line of code prints amessage to the console window

Comments

Add comments to Java programs to describewhat your program does This helps others (and you!) understand your

code

Information to include: Your name(s) Date Description Version etc.

Comments

Types of comments in Java include: Line-comments

Block comments

“Javadoc” comments

// This is a line-comment

/* This is a block-comment */

/**

* Describes a section of Java code and

* automatically generates HTML documentation

*/

Compiling a Java Program

public class Simple{ ...

Java program

JavaCompiler

000101000011001010000100010010010101010101010010

Java byte code(“.class” file)

Executing a Java Program000101000011001010000100010010010101010101010010

Java byte code(“.class” file)

000101000011001010000100010010010101010101010010

Precompiled libraries(i.e. byte code)

JavaVirtual

Machine

Keywords in Java

Java contains keywords that areunderstood by the Java compiler

e.g. public, static, class, void, etc.

Keywords are always lowercase

Keywords cannot be used for any other purpose (e.g. for class names, variable names, etc.)

Keywords in Java

abstractassertbooleanbreakbytecasecatchcharclassconstcontinuedefaultdo

doubleelseenumextendsfalseforfinalfinallyfloatgotoifimplementsimport

instanceofintinterfacelongnativenewnullpackageprivateprotectedpublicreturnshort

staticstrictfpsuperswitchsynchronizedthisthrowthrowstransienttruetryvoidvolatilewhile

What’s in a Name?

Names are important in a Java program A class name uniquely identifies a Java

program

Class names must start with a letter andcontain only letters and numbers Names could also contain underscore ‘_’ or

dollar sign ‘$’ characters, but these should be avoided

public class WelcomeToJava{ ...}

Class and Method Headers

Java programs usually contain a class header and a method header The class header identifies the name

ofthe class (i.e. program) and where it begins:

The method header identifies the name of the method (e.g. main) and where the method begins:

public class Simple{

public static void main( String[] args ) {

Java Statements

A Java statement is a line of valid Java code

Most Java statements must end in a semicolon ‘;’

Class and method headers must not endwith a semicolon

System.out.println( "Hello!" );

System.out.println( "Hello!" );System.out.print( "Welcome to" );System.out.println( " Java." );

Java Output

Use print and println to display outputto the console window

Use println to go to the next line

Sample output:

System.out.println( "Hello!" );System.out.print( "Welcome to" );System.out.println( " Java." );

Hello!Welcome to Java.

Java Output

Also use special Java escape sequences to go to the next line, tab forward, etc.

\n newlineAdvances the cursor to the next line for subsequent printing

\t tabCauses the cursor to skip over to the next tab stop

\\ backslash Causes a backslash to be printed

\'single quote

Causes a single quotation mark to be printed

\"double quote

Causes a double quotation mark to be printed

Java Output

Incorporate Java escape sequences into the text you display

Sample output:

System.out.println( "\tHello!\n" );System.out.print( "Welcome to" );System.out.println( " \"Java\"" );

Hello!

Welcome to "Java"

Java Data Types

A data type represents a set of allowed values (and operations on those values) Data type int is a numerical data type

representing integers Data type float is a numerical data type

representing floating-point numbers Data type double is a numerical data type

representing double-precision floating-point numbers

Other data types are byte, short, long,boolean, and char

Java Data Typesbyte 1

byteIntegers in the range-128 to +127

short 2 bytes

Integers in the range of-32,768 to +32,767

int 4 bytes

Integers in the range of-2,147,483,648 to +2,147,483,647

long 8 bytes

Integers in the range of-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

float 4 bytes

Floating-point numbers in the range of ±3.410E-38 to ±3.410E38, with 7 digits of accuracy

double 8 bytes

Floating-point numbers in the range of ±1.710E-308 to ±1.710E308, with 15 digits of accuracy

use int for your integers

use double for yourfloating-point numbers

Java Data Types

A data type represents a set of allowed values and operations on those values Arithmetic operators include addition (+),

subtraction (-), multiplication (*), division (/),and modulus division (%)

Binary operators require two operands (e.g. 9 * 87)

Unary operators require one operand (e.g. -37)

modulus division calculates theremainder after integer division

(e.g. 9 % 2 equals 1)

negation operator

Variables

As in mathematics, a variable is a symbolic name used to represent numeric (or other) values In Java, variables must be declared and

must have a specific data type:int x;float y;double average;long q;

specify the data type

specify the variable name

Assigning Values to Variables

Assign values to variables using the = operator: int x;

float y;double average;long q;

x = 47;y = -12.375F;average = 42.7;q = 875L;

specify the variable name

specify the value

Displaying Variables as Output

Use print and println to displaythe values of variables:

Sample output:

System.out.print( "Value of x is: " );System.out.println( x );System.out.print( "Value of y is: " );System.out.println( y );

Value of x is: 47Value of y is: -12.375

Displaying Variables as Output

Use the concatenation operator (+) to simplify your print and println statements:

Sample output:

System.out.println( "Value of x is: " + x );System.out.println( "Value of y is: " + y );

Value of x is: 47Value of y is: -12.375