java nuts and bolts variables and data types operators expressions control flow statements arrays...

18
Java Nuts and Bolts • Variables and Data Types • Operators • Expressions • Control Flow Statements • Arrays and Strings

Upload: samuel-franklin

Post on 19-Jan-2016

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

Java Nuts and Bolts

• Variables and Data Types

• Operators

• Expressions

• Control Flow Statements

• Arrays and Strings

Page 2: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

public class BasicsDemo { public static void main(String[] args) { int sum = 0; for (int current = 1; current <= 10; current++) { sum += current; } System.out.println("Sum = " + sum); } }

Page 3: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

Variables and Data Types

• Variables contain data that can change during program executiontype name

• All Variables have type, name, scope• Two Major Categories (Type):

– Primitive– Reference

• Purity: Format and Size

Page 4: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

// integers byte largestByte = Byte.MAX_VALUE; short largestShort = Short.MAX_VALUE; int largestInteger = Integer.MAX_VALUE; long largestLong = Long.MAX_VALUE;

// real numbers float largestFloat = Float.MAX_VALUE; double largestDouble = Double.MAX_VALUE;

// other primitive types char aChar = 'S'; boolean aBoolean = true;

Page 5: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

Variable Type

• Determines Values Variable can have

• Determines Operations that can be performed on a variable

• Primitive: Single value of appropriate size and format

• Reference to value or set of values that variable represents (classes, interfaces, arrays)

Page 6: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

Primitive Data Types

Type Size/Format Description(whole numbers)

byte 8-bit two's complement Byte-length integershort 16-bit two's complement Short integerint 32-bit two's complement Integerlong 64-bit two's complement Long integer

(real numbers)float 32-bit IEEE 754 Single-precision floating pointdouble 64-bit IEEE 754 Double-precision floating point

(other types)char 16-bit Unicode character A single characterboolean true or false A boolean value (true or false)

Page 7: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

Variable Names

• Program refers to variable value by name

• Composed of Unicode Characters

• Must not use keywords or boolean literals (true or false)

• Unique within scope

• Convention: begin with lowercase

• More than one word, capitalize

Page 8: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

Scope

• Block of code where variable is accessible

• Determined by location in program

• Four Categories– Member Variable– Local Variable– Method Parameter– Exception-Handler Parameter

Page 9: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings
Page 10: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

•Member Variable is a member of a class or object which

can

be declared anywhere in a class, but not in a method.

•Local Variables are declared anywhere in a method.

•Method Parameters are used to pass values to methods

(arguments)

•Exception-handler Parameters are arguments to exception

handling methods.

Page 11: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

Variable Initialization

• Local and member variables can be initialized when declared.

• final keyword: value cannot change after initialization.

• Variables must be initialized before use

• Good Programming Practice to do it at declaration

Page 12: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

Operators

• Perform a function

• Unary, binary, tertiary, …

• Unary: Prefix or Postfix (++i, i++)

• Binary: Infix (a + b)

• Returns a value

• Arithmetic, Bitwise and Logical, Assignment

Page 13: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

Expressions

• The Workers

• Application of an operator is an expression

• An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to a single value.

• Precedence

Page 14: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

postfix operators [] . (params) expr++ expr--

unary operators ++expr --expr +expr -expr ~ !

creation or cast new (type)expr

multiplicative * / %

additive + -

shift <

relational < > <= >= instanceof

equality == !=

bitwise AND &

bitwise exclusive OR ^

bitwise inclusive OR |

logical AND &&

logical OR ||

conditional ? :

assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=

Page 15: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

Control Flow

• while

• if-then-else

• switch

• for loops

• continue

Page 16: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

Statement Keyworddecision making if-else, switch-caseloop for, while, do-whileexception try-catch-finally, throwmiscellaneous break, continue, label: , return

Page 17: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

Arrays and Strings

• Array Declaration: Array is an class, so an array object must be instantiated (new)

• Instantiation allocates memory to the object

• Arrays can contain any valid Java data type

• String objects, not character arrays

• Concantenation operator (+)

Page 18: Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings

/* * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */import java.io.*;

public class Count { public static void countChars(InputStream in) throws IOException { int count = 0;

while (in.read() != -1) count++;

System.out.println("Counted " + count + " chars."); }

public static void main(String[] args) throws Exception { if (args.length >= 1) countChars(new FileInputStream(args[0])); else System.err.println("Usage: Count filename"); }}