Docsity

Java Programming Basics: Types, Variables, Operators, and Assignments, Slides of Network security

Partial preview of the text

Download Java Programming Basics: Types, Variables, Operators, and Assignments and more Slides Network security in PDF only on Docsity!

1: Types, Variables, Operators

Goal

Learn enough Java to do something

useful

Examples:

  • Simulate a natural/engineering process
  • Manipulate PDFs
  • Draw pretty graphics

The Computer

Central Processing Unit (CPU)

Input/Output (IO) Devices

Memory

CPU Instructions

z = x + y Read location x

Read location y

Add

Write to location z

Java

  • “Most popular” language
  • Runs on a “virtual machine” (JVM)
  • More complex than some (eg. Python)
  • Simpler than others (eg. C++)

Compiling Java

Source Code (.java)

Byte Code javac (^) (.class) java

Program Structure

class CLASSNAME {

public static void main ( String [] arguments ) { STATEMENTS }

}

Output

System.out.println( some String ) outputs to

the console

Example:

System.out.println(“output”);

Types

Kinds of values that can be stored and

manipulated.

boolean : Truth value ( true or false ).

int : Integer (0, 1, -47).

double : Real number (3.14, 1.0, -2.1).

String : Text (“hello”, “example”).

Variables

Named location that stores a value of one

particular type.

Form:

TYPE NAME ;

Example:

String foo;

Assignment

Can be combined with a variable

declaration.

Example:

double badPi = 3.14;

boolean isJanuary = true;

class Hello3 {

public static void main ( String [] arguments ) { String foo = "IAP 6.092" ; System. out. println ( foo ); foo = "Something else" ; System. out. println ( foo ); }

}

Order of Operations

Follows standard math rules:

  1. Parentheses
  2. Multiplication and division
  3. Addition and subtraction

class DoMath {

public static void main ( String [] arguments ) {

double score = 1.0 + 2.0 * 3.0 ;

System. out. println ( score );

score = score / 2.0 ;

System. out. println ( score );