Java Basics: Understanding Java Programming with Docsity.com, Lecture notes of Computer Security

A comprehensive introduction to java programming basics, including java programs and byte-code, skeleton java program structure, classes and objects, program syntax and style, variables, assignment and output, and more. It also covers important topics such as identifiers, primitive data types, integers, floating-point numbers, strings, and input/output.

Typology: Lecture notes

2012/2013

Uploaded on 04/22/2013

satheesh
satheesh 🇮🇳

4.5

(11)

85 documents

1 / 47

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Basics
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f

Partial preview of the text

Download Java Basics: Understanding Java Programming with Docsity.com and more Lecture notes Computer Security in PDF only on Docsity!

Java Basics

Java Programs and Byte-Code

 Source cod e is the program/instructions written in

Java (high level language).

 The Java compiler converts source code into byte-

code (low level language).

 The Java virtual machine (JVM) converts the byte-

code into machine code and executes it.

Source code: HelloWorld.java

Byte code: HelloWorld.class

Hello World

Compile (javac) Run (java)

Example Program

Test1.java:

public class Test

public static void main ( String[] args )

System.out.println (“Hello World”);

output :

Hello World

What It Means

This class/program is accessible from outside (the file)

We are going to define a class / program

The class/program is to be called Test

This set of instructions is accessible from outside (the class) and can be used immediately

The set of instructions is to be called main

Print Hello World to the screen

How we find the extra data typed in after the name of the program

Once it is done, no data is sent back for more processing

public class Test {

public static void main ( String[] args ) { System.out.println (“Hello World”); } }

Problem

 Write a program to print out the lyrics of the chorus of

Britney Spears' song “Gimme More”.

Classes and Objects

Instruction instruction instruction ...

classY

Instructiona instructionb instructionc ...

classX

Instruction instruction instruction ...

object

Instruction instruction instruction ...

object

Instructiona instructionb instructionc ...

object

Classes:

  • Templates to create objects.
  • Written by programmers and compiled.
  • Stored in files.

Objects:

  • Correspond to classes.
  • Created by JVM.
  • Stored in memory.

Program Syntax and Style

 Semicolon needed after every statement.

 Case-sensitivity

 (^) STUFF vs stuff vs STuff vs stUFF

 Indented programs are easier to read.

 Everything after // is a comment

// a sample method

public void test

System.out.println (“Hi”); // write Hi on screen

Comments

 Brief description, author, date at top of class.

 Brief description of purpose of each method (if more

than one).

 Short explanation of non-obvious parts of code within

methods.

// test program to print to screen

// hussein suleman

// 16 february 2009

public class HelloWorld

Identifiers

 In source file, HelloWorld is an identifier.

 Identifiers are used to name parts of the program.

 start with _ or letter, and followed by zero or more

of _, letter or digit

 preferred style: ClassName, everythingElseLikeThis

 Reserved words :

 class, public, void, …

 Not reserved but has special meaning:

 main, String, ...

Identifiers: Quick Quiz

 Which are valid identifiers:

 (^12345)

 (^) JanetandJustin

 (^) lots_of_money

 (^) “Hello world”

 (^) J

 (^) cc:

 Which are good identifiers?

Integers: Literals

 Literals are actual data values written into a program.

 Numerical literals can be output just like text, but after

sensible conversions:

 (^) System.out.println (12);  (^12)  (^) System.out.println (“No:” + 12);  (^) No:  (^) System.out.println (12 + 13);  (^25)  (^) System.out.println (“No:” + (12 + 13));  (^) No:

Integers: Expressions

 Common operations

 + (plus), - (minus), / (divide), * (times), % (mod)

 11 + 11 / 2 = 16 … how?

 precedence of operators:

 high: ( )

 middle: * / %

 low: + -

 left associative if equal precedence.

 integer operations when both “operands” are integers.

Integers: Types

name size smallest largest

byte 1 byte -128 127

short 2 bytes -32768 32767

int 4 bytes -2147483648 2147483647

long 8 bytes approx. -910^18 approx. 910^18

Floating-point numbers

2.6e12, 5.34e-

 Two types:

 float 4 bytes 1.4e-45 … 3.4e+

 double 8 bytes 4.9e-324 … 1.7e+

 Same precedence and meaning of operations, except

for mixed type expressions

 (^) (10 / 4.0f) * 4

 (^) Must use suffix to force calculations to be floating point!