Introduction To Programming: First Program, Types of Data, Expression

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Introduction to Programming

First Program, Types of Data, Expression

/** This is my first program Date: May 31, 2012 Author: */


class MyFirstProgram { public static void main(String[] args) { //Display Hello World! on screen System.out.println (Hello World!); } }

6/4/2012

Keywords/Reserved Words
abstract float protected generic case static inner instanceof continue do try extends operator finally private break rest if catch switch synchronized interface native else void package boolean future byvalue short import class const throw transient null final while for public goto cast super implements int long double var outer default byte return char this throws new

volatile

/** This is my second program Date: May 31, 2012 Author: */ class MySecondProgram { public static void main(String[] args) { int x = 5; int y = 10; //Compute for the sum System.out.println (Value of first variable is + x); System.out.println (Value of second variable is + y); System.out.println (Sum of two variables is + x+y); } }
4 6/4/2012

Types of Data

Literals Variables

Literals (or values)

Source code representation of a value of a

a primitive type

integer floating-point boolean character literal

a String type, or a null type

Literals: Primitive Type

Integers

Whole number Real numbers true or false Enclosed in single quotes Examples: a, 5, S, Special Characters

Floating-Point Numbers

No Comma No Space
(between unary + or and digits)

Boolean

Characters

Escape character Examples: \t, \r, \n , \0 (null character)

Literals: String

Enclosed in double quotes Series of zero or more characters Examples:


Hello, Alph@Num3r1c This is a String. She said, \Hi!\ (empty/null string)

Literals: Null Type

Has one value: the null reference

null

Literals: Summary
Numeric

Non-Numeric

Integers

Characters

Whole number

Enclosed in single quotes Special Characters


a, 5, S,
Escape character \t, \r, \n \0 (null character)

Floating-Point Numbers

Real numbers

Strings

No Comma No Space
(between unary + or and digits)

Enclosed in double quotes Series of zero or more characters Hello, Alph@Num3r1c (empty/null string)

Primitive Data Types


Type Name byte short int long float double Kind of Value Integer Integer Integer Integer Floating point Floating point Memory Size 1 byte 2 bytes 4 bytes 8 bytes 4 bytes 8 bytes Size Range -128 to 127 -32768 to 32767 -2147483648 to 2147483647 -9223372036854775808 to 9223372036854775807 3.40282347x10+38 to 1.40239847x10-45 1.7676931348623157x10+308 to 4.9406564584124654x10324

char boolean

Single character true or false

2 bytes 1 bit

all Unicode character

Identifiers (or names)


Sequence of letters, digits and underscore _ Rules:

Again, letters, digits and underscore only Cannot begin with a digit Cannot be keywords Case-sensitive
Variables names that have values that may be modified Constants names with values that cannot be modified

Used to name

Introduction to Programming
Expressions

Expressions

Arithmetic Relational Logical

Arithmetic operators

Addition Subtraction

Multiplication
Division Modulo computes for the remainder

Assignment Operator

x = y + 6 x takes the value of y + 6

Increment/Decrement Operators

++

Increment by 1 Decrement by 1

More Operators

+=

x += 5

is the same as

x = x + 5;

x -= 5

is the same as

x = x - 5;

*= /= %=

Relational Operators

<

Less than Less than or equal Greater than Greater than or equal equal Not equal

<=

>

>=

==

!=

Logical Operators

Not or Negation

&&

And True, iff both operands are true.

||

Or False, iff both operands are false.

Evaluating Expressions

Operator Precedence

Determines the priority/order of operators Determines the direction of evaluation Example: a/b/c

Associativity

Is it (a/b)/c or a/(b/c)? What if a = 11, b = 2, and c = 5?

OPERATOR

ASSOCIATIVITY right to left left to right left to right left to right left to right left to right

n-ary unary binary binary binary binary binary

!, +, , ++, *, /, % +, <, <=, >, >= ==, != &&

||
=, +=, and all combined assignment

left to right
right to left

binary
Binary

Programming Exercises

Create a Java program that computes for the perimeter and area of a square and triangle. The perimeter for both square and triangle is computed as getting the sum of all sides. The area, on the other hand, is computed differently. Area of a square = s2, where s is a side; and triangle is bh, where b is the base and h is the height. Supply in your program the values necessary for both square and triangle Create a Java program which displays the individual digits of 3-digit number separately,, and computes for the sum of all digits.

End of Discussion

You might also like