You are on page 1of 32

Java: An Introduction

Sanjay Saha
CSI 211: Object Oriented Programming, Fall 2016
Department of Computer Science and Engineering
United International University

United International University

CSI 211: Object Oriented Programming

Course Teacher
Sanjay Saha
Lecturer, Department of CSE
United International University
Room Number: 4609
Email: sanjay@cse.uiu.ac.bd
Contact No. 01712758358
B. Sc. in CSE, DU (2014)
Continuing M.Sc. in CSE, DU (2016)
Area of Interests
Data Mining
Vehicular Networks
Web Application Development
United International University

CSI 211: Object Oriented Programming

Class Information
Course Website:
https:
//sites.google.com/a/cse.uiu.ac.bd/sanjay/courses/oop
Schedule
Monday, Wednesday, 9:55AM-11:15PM and 12:45PM-2:00PM
Grading
Assignment+Class Performance - 10%
Class Test - 20 % (best 2)
Mid Exam - 30%
Final Exam - 40%
United International University

CSI 211: Object Oriented Programming

Books

Text Book
Java: The Complete Reference, 9th Edition, Herbert Schildt
References
1 Java: How to Program, 9th Edition, Deitel and Deitel
2

Effective Java, Josua Bloch, Second Edition

Head First Java, Second Edition, Kathy and Bates

United International University

CSI 211: Object Oriented Programming

Java: Where it came from?

Fortran, Cobol, Pascal, Ada, Basic, C, C++, Objective C, Visual


Basic, Python, Ruby, Java!
The language was initially called Oak after an oak tree that stood
outside Goslings office. Later the project went by the name Green
and was finally renamed Java, from Java coffee.
There are 9 million Java developers worldwide, 3 billion mobile
phones run Java, 100 percent of Blu-ray disc players ship with
Java, 97 percent of enterprise desktops run Java.
United International University

CSI 211: Object Oriented Programming

What is Java?

A simple, object-oriented, distributed, interpreted, robust, secure,


architecture neutral, portable, high-performance, multithreaded,
and dynamic language!
Object-Oriented
Pure OOP!
No free functions.
All code belong to some class.
Syntax derived from C.
Object Oriented features are influenced by C++.

United International University

CSI 211: Object Oriented Programming

Java - The Most Popular

United International University

CSI 211: Object Oriented Programming

Java tumi kar?

United International University

CSI 211: Object Oriented Programming

Java Editions

Java Standard Edition - used for developing desktop


applications and networking applications

Java Enterprise Edition - used for developing large scale,


distributed networking applications and web-based applications

Java Micro Edition - used for developing applications for


resource-constrained embedded devices

United International University

CSI 211: Object Oriented Programming

How Java Works

United International University

CSI 211: Object Oriented Programming

10

Java Development Environment


JDK
Use latest JDK (JDK 8 from Oracle or OpenJDK)
$ javac Welcome.java - Compiles a java file and produces
Welcom.class (byte code)
$ java Welcome - It runs java byte code on native machine
1
2
3

Class Loader
Verifier
JVM

Java IDE
1 IntelliJ IDEA
2

Eclipse

Netbeans

United International University

CSI 211: Object Oriented Programming

11

The First Program - Guess the Output

United International University

CSI 211: Object Oriented Programming

12

Source Code Naming Conventions


1

All java source code files must end in .java

Each .java file can contain only one public class

The name of the file should be exactly same as the public


class with extension .java

Java is case sensitive

Do not use abbreviations in the name of the class

If the class name contains multiple words then capitalize the


first letter of each word ex. HelloWorld.java

Its a good practice to write one class per file

Name of the main class should match the name of the file
that holds the program

United International University

CSI 211: Object Oriented Programming

13

Java Comments

Three type of comments


1

/* standard c coments work*/

// C++ style comments

/** Java doc comments


*/

United International University

CSI 211: Object Oriented Programming

14

A Closer Look
public static void main(String args[])
main - the starting point for any java program
public - to make this method accessible to all
static - to call this method instantiation is not needed. JVM
calls Classname.main
void - the main method does not return anything
String args[] - An array of string objects that holds the
command line arguments passed to the application

System.out.print()
Used to print a text
System - is a class in Java API (Application Program
Interface)
out - represents the standard output, static member of
System, part of API

System.out.println()
United International University

CSI 211: Object Oriented Programming

15

Escape Sequences

United International University

CSI 211: Object Oriented Programming

16

Another Program!

United International University

CSI 211: Object Oriented Programming

17

A Closer Look

import java.util.Scanner;
Java class library, or the Java Application Programming
Interface (Java API)
import declaration that helps the compiler locate a class thats
used in this program

Scanner myScanner=new Scanner(System.in);


A Scanner enables a program to read data
The standard input object, System.in, enables applications to
read bytes of data typed by the user.
The Scanner translates these bytes into types (like int )

United International University

CSI 211: Object Oriented Programming

18

Java Tips

Capitalize Class names


Always add comments to your code
Always close the brackets immediately you start them
Add a comment at all the ending brackets
Use proper indentation in your code
For errors check the line number and lines before it
All import declarations must appear before the first class
declaration in the file.

United International University

CSI 211: Object Oriented Programming

19

Declaration and Naming


Declare each variable in its own declaration.
Naming Convention
Classes: Nouns, in mixed case with the first letter of each
internal word capitalized. Example: CamelCase, HelloWorld.
Variables: Mixed case with a lowercase first letter. Internal
words start with capital letters. Variable names should not
start with underscore or dollar sign $ characters, even
though both are allowed. Example: myInteger, yourHome.
Methods/Functions: Methods should be verbs (similar to
variables). Example: getSize(), setSize().
Constants: Constants should be all uppercase with words
separated by underscores ( ). Example: MAX SIZE
http://www.oracle.com/technetwork/java/codeconventions-135099.html
United International University

CSI 211: Object Oriented Programming

20

Data Types
8 primitive types / 4 groups
1

Boolean: boolean to represent true/false values.

Characters: char to represent symbols in character set and


digits.

Integers: byte, short, int and long to represent


whole-valued signed numbers. There are no unsigned integers
in Java (differ with C).

Floating Point Numbers: float and double to represent


fractional numbers.

All data types have a strictly defined range, do not depend on


execution environment (unlike C).
United International University

CSI 211: Object Oriented Programming

21

Integers and Floats

byte: Useful when working with raw binary data.

United International University

CSI 211: Object Oriented Programming

22

Characters
Java char is 16 bit and stores in unicode format.

integer literals: binary (0b10101), octal (074545), hexa (0x3434)


underscore is allowed (int x = 123 323 232 34343L;) JDK 7
http://unicode.org/charts/PDF/U0980.pdf
United International University

CSI 211: Object Oriented Programming

23

Operators: Arithmetic

United International University

CSI 211: Object Oriented Programming

24

Operators: Bitwise

United International University

CSI 211: Object Oriented Programming

25

Operators: Relational

C style
int flag=0;
if(flag)
{
\\ do something
}
United International University

Java style
int flag=0;
if(flag != 0)
{
\\ do something
}

CSI 211: Object Oriented Programming

26

Operators: Logical

United International University

CSI 211: Object Oriented Programming

27

Operators: Precedence

United International University

CSI 211: Object Oriented Programming

28

Scope of Variables

United International University

CSI 211: Object Oriented Programming

29

Casting

United International University

CSI 211: Object Oriented Programming

30

Reading

Java: How to Program Chapter 2


Java: The Complete Reference Chapter 1,2,3
Resources
http://cscircles.cemc.uwaterloo.ca/java visualize/
http://www.tutorialspoint.com/java/
https://thenewboston.com/videos.php?cat=31

United International University

CSI 211: Object Oriented Programming

31

Thats it!

Thank you

United International University

CSI 211: Object Oriented Programming

32

You might also like