Docsity

Java Programming for Beginners: Understanding Expressions, Operators, and Variables - Prof, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
1
Introduction to Programming
with Java, for Beginners
Process of Programming
Fundamentals:
• Comments & Literals
• Operators
• Primitive Types & Variables
• Expressions/Statements
• Strings
ESE112 2/25
Process of Computer Programming
To come up with a computation solution
Philosophy: program in increments
Edit
(Syntax + Semantics)
Compile
Run
(observe your output)
Compile-time
or Syntax Error
Run-time
or
Semantic
Error
ESE112 3/25
IDE
Use Integrated Development Environment
A software program that makes it easier to
write, compile and run programs
We’re going to use the free Dr. Java IDE
Setting up Dr Java on your personal PC
http://www.cis.upenn.edu/~palsetia/java/installDrJava.html
ESE112 4/25
Comments
Comments are used to make code more
understandable to humans
Java Compiler ignores comments
// this is a single line comment
/* this is
* a multi-line
* comment
*/
pf3
pf4
pf5

Partial preview of the text

Download Java Programming for Beginners: Understanding Expressions, Operators, and Variables - Prof and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Introduction to Programming

with Java, for Beginners

Process of ProgrammingFundamentals:

  • Comments & Literals• Operators• Primitive Types & Variables• Expressions/Statements• Strings

ESE

2/

Process of Computer Programming

„

To come up with a computation solution

„

Philosophy: program in increments

Edit

(Syntax + Semantics)

Compile

Run

(observe your output)

Compile-time

or Syntax Error

Run-time

or

Semantic

Error

ESE

3/

IDE

Use

I

ntegrated

D

evelopment

E

nvironment

„

A software program that makes it easier towrite, compile and run programs

„

We’re going to use the free Dr. Java IDE

Setting up Dr Java on your personal PC

„

http://www.cis.upenn.edu/~palsetia/java/installDrJava.html

ESE

4/

Comments

„

Comments are used to make code moreunderstandable to humans

„

Java Compiler ignores comments

// this is a single line comment

/* this is

* a multi-line* comment*/

ESE

5/

Literals

„

Literals are the

values

we write in a

conventional form whose value is obvious

// An integer has no decimal point

10.5 // a floating point double‘a’

// a character has single quotes

true // The boolean literals are of two types: true, false“hello world” // A string literal

ESE

6/

Arithmetic Operators

to indicate addition

-

to indicate subtraction


to indicate multiplication

/

to indicate division

%

to indicate remainder of a division

(integers only)

parentheses

( )

to indicate the order in

which to do things

ESE

7/

Relational Operators

==

equal to

!=

not equal to

<

less than

>

greater than

<=

less than equal to

>=

greater than equal to

Note: Arithmetic

comparisons

result in a

Boolean

value of

true

or

false

ESE

8/

Boolean or Logical Operators

„

In English, sentences conditions can be formedusing "and", "or", and "not"

„

E.g. If there is a test and you did not study for it...

„

In Java

„

-> OR operator

¾

true if either operand* is true

„

-> AND operator

true only if both operands are true

„

-> NOT operator ¾

Is a unary operator – applied to only one operand

Reverses the truth value of its operand

*Operand: a quantity upon which a mathematical

operation is performed

ESE

13/

Storage Space for Numerics

„

Numeric types in Java are characterized by their

size

how much memory they occupy

„

Integer types

„

Floating point types

0:

2 bytes

char

-2147483648:

4 bytes

int

range

size

type

smallest > 01.4E-454.9E-

1.7E

8 bytes

double

3.4E

4 byte

float

largest

size

ESE

14/

Another Important Type: String

„

A

String

is an

Object

, not a primitive type

„

Java also has objects - cover objects later

„

String is composed of zero or more

char

s

„

A String is a sequence of characters enclosedby double quotes

"Java"

"3 Stooges"

„

means concatenation for strings

"3" + " " + "Stooges"

3 Stooges

„

Automatic conversion of numbers to strings

3 + " " + "Stooges"

3 Stooges

ESE

15/

Variables

„

A variable is a named place in memory used tostore a value

„

Variable must always be associated with

type

„

It tells the computer how much space toreserve for the variable

„

The value stored can vary over time

ESE

16/

Identifiers

„

Identifiers are names that you as a coder make up

„

Variable names

„

Also class and method names – next topic to cover

„

Variable names

„

Java Rule

: May consist of alphanumeric characters and

the underscore

_

and must start with a

letter

„

Style Rule

: Should be a noun that starts with an

lowercase letter

„

E.g.

sum, average

„

If the name has multiple words, capitalize the startof every word except the first (style rule)

„

E.g.

firstName, lastName

ESE

17/

Declaring variables

„

All variables must be

declared

before being used

„

With a declaration

statement

„

Declaration statement

„

Specifies the

type

of the variable, followed by

descriptive

variable name

, followed by

semicolon

„

Examples:

int

seats;

double

averageHeight;

boolean

isFriday;

String

houseName;

ESE

18/

Storing value into Variables

„

To store values into variable we use the

assignment

operator i.e. “

„

Variable = Expression; ->

assignment statement

„

Important

„

Assignment statement must end with a semicolon(;)

„

When a variable is assigned a value, the old value isdiscarded and totally forgotten

„

Examples:

seats = 150;averageHeight = (2.1 + 1.74 + 1.58)/3;isFriday = true;houseName = ”gryffindor"

;

ESE

19/

Variable value and type

The

value

of a variable may be changed

x = 57;

However its

type

may not

x = true;

// this causes an error, compiler will

complain

ESE

20/

Initializing Variables

It’s good idea to declare and initialize avariable in one statement

double milesPerHour = 60.5;

String myName = ”Diana Palsetia";

ESE

25/

Recap

„

An Expression

„

Has a value

„

Consists literals and operators – FOR NOW!

„

A Statement (declaration and assignment)

„

Must end with semicolon(;)

„

Tells or commands the computer to do something

„

Comments are ignored by the computer

„

They are explanations of your program for humanbeings to read