Lecture 01

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

Java: An Introduction

Swakkhar Shatabda

CSI 211: Object Oriented Programming, Summer 2016


Department of Computer Science and Engineering
United International University

United International University CSI 211: Object Oriented Programming 1


Course Teacher

Dr. Swakkhar Shatabda


Assistant Professor, Department of CSE
United International University
Room Number: 104
Email: [email protected]
Contact No. 01776195310
B. Sc. in CSE, BUET (2007)
PhD, Griffith Univeristy, Australia (2014)
Area of Research
Artificial Intelligence Algorithms
Constraint Programming
Combinatorial Optimization
Algorithms in Bioinformatics
United International University CSI 211: Object Oriented Programming 2
Class Information

Course Website:
swakkhar.github.io/java.html

Schedule
Sunday, Tuesday, Room # 105 11:20AM-12:40PM

Grading
Assignment+Class Performance - 10%
Class Test - 20 % (best 2)
Mid Exam - 30%
Final Exam - 40%

United International University CSI 211: Object Oriented Programming 3


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


3 Head First Java, Second Edition, Kathy and Bates

United International University CSI 211: Object Oriented Programming 4


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 Gosling’s 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 5
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 6


Java - The Most Popular

United International University CSI 211: Object Oriented Programming 7


Java tumi kar?

United International University CSI 211: Object Oriented Programming 8


Java Editions

1 Java Standard Edition - used for developing desktop


applications and networking applications
2 Java Enterprise Edition - used for developing large scale,
distributed networking applications and web-based applications
3 Java Micro Edition - used for developing applications for
resource-constrained embedded devices

United International University CSI 211: Object Oriented Programming 9


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 Class Loader
2 Verifier
3 JVM

Java IDE
1 IntelliJ IDEA

2 Eclipse
3 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


2 Each .java file can contain only one public class
3 The name of the file should be exactly same as the public
class with extension .java
4 Java is case sensitive
5 Do not use abbreviations in the name of the class
6 If the class name contains multiple words then capitalize the
first letter of each word ex. HelloWorld.java
7 Its a good practice to write one class per file
8 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*/
2 // C++ style comments
3 /** 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.
2 Characters: char to represent symbols in character set and
digits.
3 Integers: byte, short, int and long to represent
whole-valued signed numbers. There are no unsigned integers
in Java (differ with C).
4 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

1 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 Java style


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

United International University 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