CSC 201-Lecture 2

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

CSC 201-Lecture 2

Agenda
• Review of Lecture – 1
• Java program structure
• Data Types in Java
• Next Lab Session
Review Of Lecture-1
• Data
• Algorithm
• Flowchart
• Introduction to Programming
• Terms: Software, Hardware.
Structure of a Java Program
• Create a program.
• Compile a program.
– What is a compiler?
• A compiler is an application that translates source
code of a program into machine readable code.
• Run/Execute the program.
How Java Program is Compiled?
• To compile: javac <filename>.java
• To Execute: java <filename>
• When you compile a Java program, the
output is NOT exe file, but a .class file
Compiling & Executing Concepts
• JDK : Java Development Kit
• JVM : Java Virtual Machine
• JRE : Java Runtime Environment
Imperative part of Java
• Comment forms in Java
/* */ Block Comment
// Single line comment

• Java is case sensitive.


Variables, Constants, Identifiers
• Variable : A variable is a storage location.
It holds a value of that type.
Ex: a , a1
• Constants : Constants are variables that
do not change.
Ex: n1 = 2;
• Identifiers : Similar to variable
synonymously. It is usually any place in
memory where data resides.
Statements
int a, b; /* Associates a variable name
with a type at compile time*/

a = 12;/* assignment statement ,


initialization*/

int c = a * b; /* decl stmt + assignment


stmt*/
Primitive Data Types
Java is a statically typed language-means
you have to declare a variable before they
can be used.
• What is a Data Type?

• How to define a data type?


data-type variable-name;
Ex: int n1;
8 Primitive Data Types
Byte : -128 to 127(inclusive)
Short : -32768 to 32767
Int : -2,147,483,648 to
2,147,483,647
Long : -2^63 to 2^63 -1
Float : 6 significant digits
Double : 15 significant digits
Bool : True or False
char : unicode
• Character constants are enclosed in ‘ ’.
• String constants consists of sequence of
characters enclosed in “ “.
• Escape Sequences are used to represent
certain character constants.
‘\n’ for newline
‘\\’ backslash character.
Variables
• A variable is a value that can change
depending upon your program.
Example:
int n1, n2;
n1 = 2;
n2 = 3;
Operators in Java
• Assignment Operators
Ex : ==, =+, *=, -=
• Binary Arithmetic operators
Ex : +, - , *, /
• Unary operators
Ex : ++, --
• Type conversions (later)
Lab Session
• Creating a .java program.
• Saving the program.
• Compiling : javac myprogram.java
.class file is created
• Run: java myprogram
• Reading Assignment
Textbook Ch 1.1 to 1.3
Screenshots: How to write a
program in a notepad/Textpad
Helloworld.java
public class helloworld
{
public static void main(String[] args)
{
System.out.println("Hello CSC201 !!");
}
}
Addition using args()
User gives input from cmd prompt
public class addition
{
public static void main(String[] args)
{
int n1, n2, result;
n1 = Integer.parseInt(args[0]);
n2 = Integer.parseInt(args[1]);
result = n1 + n2;
System.out.println("After adding the two numbers,
result is :" + result);
}
}

You might also like