0% found this document useful (0 votes)
30 views

Final Slide Java

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Final Slide Java

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

First lesson in this class

• Computers can’t read minds.

• Computers don’t make mistakes.

• If the computer is not doing what you want, it’s because YOU made a
mistake.
History of Java
• Java was developed by James Gosling, who is known as the father
of Java, in 1995. James Gosling and his team members started the
project in the early '90s.

• James Gosling and his team initiated the Java language project in
June 1991. The small team of sun engineers called Green Team.

• Developed and maintained by Sun Microsystems


• Originally called Oak
What is Java?
• Java is a popular programming language, created in 1995.
• It is owned by Oracle, and more than 3 billion devices run Java.
• It is used for:
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Games
• And much, much more!
Why Use Java?
• Java works on different platforms (Windows, Mac, Linux,
etc.)
• It is one of the most popular programming language in the
world
• It has a large demand in the current job market
• It is easy to learn and simple to use
• It is secure, fast and powerful
• As Java is close to C++ and C#, it makes it easy for
programmers to switch to Java or vice versa
The Virtual Machine
• Java is both compiled and interpreted
• Source code is compiled into Java bytecode
• Which is then interpreted by the Java Virtual Machine
(JVM)
• Therefore bytecode is machine code for the JVM
Get Started
• It is not necessary to have any prior programming
experience.
Your first Java program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}

• File must be named Hello.java


Bigger Java program!
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
System.out.println();
System.out.println("This program produces");
System.out.println("four lines of output");
}
}
• Its output:
Hello, world!

This program produces


four lines of output

• console: Text box into which


Running a program
1. Write it.
• code or source code: The set of instructions in a program.

2. Compile it.
• compile: Translate a program from one language to another.
• byte code: The Java compiler converts your code into a format named byte code
that runs on many computer types.

3. Run (execute) it.


• output: The messages printed to the user by a program.

source code byte code output


compile run
Structure of a Java program
class: a program

public class name {


public static void main(String[] args) {
statement;
statement;
... method: a named group
statement; of statements
}
}
statement: a command to be executed

Every executable Java program consists of a class,


that contains a method named main,
that contains the statements (commands) to be executed.
Identifier:
• A name in java program is called identifier. It may be class name,
method name, variable name and label name.

class: a program

public class name {


public static void main(String[] args) {
int x = 10;
}
}
method: a named group
Variable name
of statements
Rules to define java identifiers :
• Rule 1: The only allowed characters in java identifiers are:
1) a to z
2) A to Z
3) 0 to 9
4) _ (underscore)
5) $
• Rule 2: If we are using any other character we will get compile time error.
Example:
1) total_number-------valid
2) Total#------------------invalid
• Rule 3: identifiers are not allowed to starts with digit.
Example:
1) ABC123---------valid
2) 123ABC---------invalid
• Rule 4: java identifiers are case sensitive of course java language itself
treated as case sensitive language.
Example:
class Test{
int number=10;
int Number=20;
int NUMBER=20; we can differentiate with case.
int NuMbEr=30;
}
• Rule 5: There is no length limit for java identifiers but it is
not recommended to take more than 15 lengths.

• Rule 6: We can't use reserved words as identifiers.

Example:

int if=10; --------------invalid


Which of the following are valid java
identifiers?
1. _$_(Valid)
2. Ca$h(Valid)
3. Java2share(Valid)
4. all@hands(invalid)
5. 123abc(invalid)
6. Total#(invalid)
7. Int(Valid)
8. Integer(Valid)
9. int(invalid)
10. total123(Valid)
Reserved Words:
• keyword: An identifier that you cannot use because it already has a
reserved meaning in Java.
• Diagram:
Reserved words for data types: (8)

1) byte 5) float

2) short 6) double

3) int 7) char

4) long 8) boolean
Reserved words for flow control:(11)

1) if 6) for

2) else 7) do

3) switch 8) while

4) case 9) break

5) default 10) continue

11) return
Keywords for modifiers:(11)

1) public 7) synchronized

2) private 8) native

3) protected 9) strictfp(1.2 version)

4) static 10) transient

5) final 11) volatile

6) abstract
Keywords for exception handling:(6)

1) try 4) throw

2) catch 5) throws

3) finally 6) assert(1.4 version)


Class related keywords:(6)

1) class 4) extends

2) package 5) implements

3) import 6) interface
Object related keywords:(4)

1) new

2) instanceof

3) super

4) this
Void return type keyword:
• If a method won't return anything compulsory that method should be
declared with the void return type in java but it is optional in C++.
1) Void

Unused keywords:
goto: Create several problems in old languages and hence it is banned in java.

Const: Use final instead of this.


By mistake if we are using these keywords in our program we will get compile
time error.
Reserved literals:
1) true values for boolean data type.
2) false values for boolean data type.
3) null----------------- default value for object reference.
Enum:
• This keyword introduced in 1.5v to define a group of named constants
Example:
enum Beer
{
KF, RC, KO, FO;
}
Note:

• All reserved words in java contain only lowercase alphabet


symbols.
• Because Java is case-sensitive, you could technically use
Class or cLaSs as identifiers, but this is very confusing
and thus strongly discouraged.
Which of the following are valid java keywords?

1. public(valid) 4. main(invalid)

2. static(valid)

3. void(valid)
Data types:
• Every variable has a type, every expression has a
type and all types are strictly define more over every
assignment should be checked by the compiler by
the type compatibility hence java language is
considered as strongly typed programming language.
Diagram:

• Except Boolean and char all remaining data types are considered as signed data types
Integral data types :

Byte:
Size: 1byte (8bits)
Maxvalue: +127
Minvalue:-128
Range:-128to 127[to]
Example:
byte b=10;
byte b2=130;//C.E:possible loss of precision
found : int
required : byte
byte b=10.5;//C.E:possible loss of precision
byte b=true;//C.E:incompatible types
byte b=“Ali";//C.E:incompatible types
found : java.lang.String
required : byte
Short:

The most rarely used data type in java is short.

Size: 2 bytes

Range: -32768 to 32767( to )

Example:

short s=130;

short s=32768;//C.E:possible loss of precision


int:
This is most commonly used data type in java.

Size: 4 bytes

Range:-2147483648 to 2147483647 ( to )

Example:

int i=130;

int i=10.5;//C.E:possible loss of precision

int i=true;//C.E:incompatible types


long:
• Whenever int is not enough to hold big values then we should go for
long data type.

Size: 8 bytes

Range: to

Note: All the above data types (byte, short, int and long) can be used to
represent whole numbers. If we want to represent real numbers then
we should go for floating point data types.
Floating Point Data types:
float double

• If we want to 5 to 6 decimal • If we want to 14 to 15 decimal places


places of accuracy then we of accuracy then we should go for
should go for float. double.

• Size: 4 bytes. • Size: 8 bytes.

• Range: -3.4e38 to 3.4e38. • -1.7e308 to 1.7e308.

• float follows single precision. • double follows double precision.


boolean data type:

• Size: Not applicable (virtual machine dependent)

• Range: Not applicable but allowed values are true or false.


Which of the following boolean declarations are
valid?
Example 1:

boolean b=true;
boolean b=True;//C.E:cannot find symbol
boolean b="True";//C.E:incompatible types
boolean b=0;//C.E:incompatible types
Char data type:
• Size: 2 bytes
• Range: 0 to 65535
Example:
char ch1=97;
char ch2=65536;//C.E:possible loss of precision
Summary of java primitive data type:
Data Size Range Corresponding Default value
type Wrapper class

byte 1 byte to (-128 to 127) Byte 0

short 2 bytes to (-32768 to 32767) Short 0

int 4 bytes to (-2147483648 to 2147483647) Integer 0

long 8 bytes to Long 0


float 4 bytes -3.4e38 to 3.4e38 Float 0.0
double 8 bytes -1.7e308 to 1.7e308 Double 0.0
boolean Not Not applicable(but allowed values true| Boolean false
applicable false)

char 2 bytes 0 to 65535 Character 0(represents blank space)

You might also like