Sei sulla pagina 1di 0

Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2005. All rights reserved.

No part of this material may be reproduced and sold.


1
Introduction to Programming
Introduction to Programming
(
(
Using Java)
Using Java)
Nandika Kasun
University of Colombo School of Computing (UCSC)
University of Colombo School of Computing (UCSC)
University of Colombo
University of Colombo
Sri Lanka
Sri Lanka
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2005. All rights reserved. No part of this material may be reproduced and sold.
2
class Addition {
public static void main(String[] args) {
int a=12;
int b=13;
int c;
c=a+b;
System.out.println(a+b=+c);
}
}
Statements, Expressions & Variables
Statements, Expressions & Variables
Write a simple Java program which adds 12
and 13, and displays the result on the screen.
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2005. All rights reserved. No part of this material may be reproduced and sold.
3

Statements
Statements
are used to accomplish simplest tasks in Java
forms simplest Java operations.
can be single line or Span to multiple lines.
does not necessarily return a value.
System.out.println(a+b=+c);
Statements, Expressions & Variables
Statements, Expressions & Variables
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2005. All rights reserved. No part of this material may be reproduced and sold.
4

Expressions
Expressions
simplest form of Statements.
returns a value when evaluated.
can be assigned to a variable or can be tested in
Java statements.
most expressions are a combination of
Operators & Operands
c=a+b;
Statements, Expressions & Variables
Statements, Expressions & Variables
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2005. All rights reserved. No part of this material may be reproduced and sold.
5

What is a Variable ?
What is a Variable ?
Variables are locations in memory in which
values can be stored.
Each Variable has a Type, Name and a Value
After Declaring, it can be used to store values
of the appropriate Type
Statements, Expressions & Variables
Statements, Expressions & Variables
int a=12;
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2005. All rights reserved. No part of this material may be reproduced and sold.
6
Variables can be of reference type or Primitive
Type.
There are Eight Primitive Data Types in Java.
Integer Types (4)
character
Floating Point types (2)
boolean
Statements, Expressions & Variables
Statements, Expressions & Variables
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2005. All rights reserved. No part of this material may be reproduced and sold.
7
Integer Types Floating Point Types
Type Bit Size
float 32
double 64
Type
byte
long
short
int
8
64
32
16
Bit Size
Statements, Expressions & Variables
Statements, Expressions & Variables
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2005. All rights reserved. No part of this material may be reproduced and sold.
8

Variable Naming
Variable Naming
Can only Start with a Letter, Underscore (_) ,
or Dollar Sign ($)
After the first letter Name can include any
letter or Number but cannot include Symbols
such as %, @, * and so on.
Names are Case Sensitive (Value,VALUE,
& value are different)
Statements, Expressions & Variables
Statements, Expressions & Variables
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2005. All rights reserved. No part of this material may be reproduced and sold.
9
Use meaningful Names such as
number, areaOfCircle, firstName
Illegal Names
1More, #Two, @Two
Legal Names
timeOfDay, temp_val, $_
Statements, Expressions & Variables
Statements, Expressions & Variables
Convention is to start a variable with a lower case
If the name is made up of several words, then
from the second word onwards the first letter of
the word is capital.
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2005. All rights reserved. No part of this material may be reproduced and sold.
10
There are three types of comments in Java
Single line comments(C++ Style )
Started with //
Comments in Java
Comments in Java
Example
// This is a Single line Comment
Multi Line Comments
Started with /* and Ended with */
Example
/* This is a
Multi line Comment */
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2005. All rights reserved. No part of this material may be reproduced and sold.
11
Comments that can be used in Automatic Class
Documentation by javadoc tool
Started with /** and Ended with */
Can Span Multiple lines as well
Comments
Comments
Example
/** The Integer class Encapsulates
primitive Data Type int .. */
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2005. All rights reserved. No part of this material may be reproduced and sold.
12
Literal is an explicit value used by a program
Literal can be either
Integer (3,100,4L)
Floating Point (3.14f, 6.023e+23d)
Character (b, \n, \u15e)
boolean (true,false)
String (This is a String Literal )
Literals
Literals
Kasun@ Kasun@cmb cmb.ac. .ac.lk lk UCSC 2005. All rights reserved. No part of this material may be reproduced and sold.
13
Summary
Summary
Today we learned Java statements,
expressions and variables
Exercise:
Exercise:
Write a simple Java program which divides 12 by 5 and
displays the result on the screen.
Write a simple Java program multiplies 12.4 and 45678932.
You should display the result on the screen up to two decimal
points
Write a program which converts inches to centimeters (1m=
39 inches)

Potrebbero piacerti anche