www.SunilOS.com 1
JAVA BASICS
Core Concepts
www.sunilos.com
www.raystec.com
www.SunilOS.com 2
Java Program is a Class
public class HelloJava {
…
}
A class may contain multiple attributes (variables) and
methods (functions).
An executable class will have default ‘main’ method
that is called by JVM to execute the program.
www.SunilOS.com 3
My First Program - HelloJava
public class HelloJava {
o public static void main(String[] args) {
o System.out.println("Hello Java”);
o }
}
public, class, static, and void are keywords.
Keywords are always written in small letters.
System.out object represents console
www.SunilOS.com 4
Define a variable
public class HelloJava {
o public static void main(String[] args) {
o String name = “Vijay”;
o System.out.println(name);
o }
}
www.SunilOS.com 5
Data Types
int
long
byte
short
float
double
1
2
4
8
4
8
-128, +127
-9.223E18, +9.223E18
-32768, +32767
-2147483648, +2147483647
+3.4 E+38
+1.7 E+308
Type Size Byte Range
char 2 0, 65535
boolean 1 true, false
0
0
0
0
0
0
0
false
Default
www.SunilOS.com 6
Add two numbers
public class Add {
 public static void main(String[] args) {
o int a = 5;
o int b = 10;
o int sum = a + b;
o System.out.println("Sum is " + sum);
 }
}
www.SunilOS.com 7
Can I buy Pizza ?
public class Add{
 public static void main(String[] args) {
o int rs = 110;
o if( rs > 100 ){
o System.out.println(“Wow! I can buy Pizza ”);
o }else{
o System.out.println(“Oh! I can not buy Pizza ”);
o }
o }
}
www.SunilOS.com 8
Other operators
()
++
--
!
~
instance of
*
/
%
+
-
<<
>>
>>>
<
>
<=
>=
==
!=
&
^
|
&&
||
?:
=
op=
www.SunilOS.com 9
Print Hello 5 times - for
public class HelloFor {
public static void main(String[] args) {
o for (int i = 0; i < 5; i++) {
 System.out.println(i + "Hello");
o }
o }
}
www.SunilOS.com 10
Print Hello 5 times – while loop
public class HelloWhile {
public static void main(String[] args) {
o int i = 0;
o while (i < 5) {
 System.out.println(i + "Hello");
 i++;
o }
}
}
www.SunilOS.com 11
Print Hello 5 times – do-while
 public class HelloDoWhile {
 public static void main(String[] args) {
o int i = 0;
o do {
 System.out.println( i+ " Hello");
 i++;
o } while (i < 5);
 }
 }
www.SunilOS.com 12
10
List of elements a.k.a. Array
20
[0]
18
..
10
8
6
4
2
[1]
[8]
[9]
[2]
[3]
[4]
[n]
length
int[] list = new int[10];
list[0] =2;
list[1] =4;
….
Or
int[] list = {2,4,6,8,10,12,14,16,18,20};
www.SunilOS.com 13
Foreach statement
public class HelloFor {
public static void main(String[] args) {
o int[] list={ 2, 4, 6, 8, 10};
o System.out.println(“Length: ” + list.length);
o for (int element : list) {
 System.out.println(element);
o }
o }
}
www.SunilOS.com 14
Handle a String
 String name = "Vijay Dinanath Chouhan";
 S.o.p(" String Length- " + name.length());
 S.o.p(" 7th character is- " + name.charAt(6));
 S.o.p(" Dina index is- " + name.indexOf("Dina"));
 S.o.p(" First i Position- " + name.indexOf("i"));
 S.o.p(" Last i Position- " + name.lastIndexOf("i"));
 S.o.p(" a is replaced by b- " + name.replace("a", "b"));
 S.o.p(" All a is replaced by b- “ + name.replaceAll("a", "b"));
 S.o.p(“ Chhota vijay- " + name.toLowerCase());
 S.o.p(" Bada vijay- " + name.toUpperCase());
 S.o.p(" Starts With Vijay- " + name.startsWith("Vijay"));
 S.o.p(" Ends with han- " + name.endsWith("han"));
 S.o.p(" Substring- " + name.substring(6));
Note : S.o.p = System.out.println
www.SunilOS.com 15
Mathematical operations
 public static void main(String[] args) {
o S.o.p(“ Mathematics functions");
o S.o.p(" Max 2,5 - " + Math.max(2,5));
o S.o.p(" Min 2,5 - " + Math.min(2,5));
o S.o.p(" Absolute 3.7 - " + Math.abs(3.7));
o S.o.p(" Exp 10 - " + Math.exp(10));
o S.o.p(" Random Number- " + Math.random());
o S.o.p(" Square Root- " + Math.sqrt(4));
 }
 Note : S.o.p = System.out.println
www.SunilOS.com 16
Handle a date
 import java.util.*;
 public class TestDate {
 public static void main(String[] args) {
o Date d = new Date();
o S.o.p("Date : " +d);
o S.o.p ("Long Time : " +d.getTime());
 }
 Output
o Date : Mon Jan 04 00:35:53 IST 2010
o Long Time : 1262545553156
www.SunilOS.com 17
Format a Date
 import java.util.*; import java.text.SimpleDateFormat;
 public class TestDateFormat{
 public static void main(String[] args) {
o Date d = new Date();
o SimpleDateFormat format= new SimpleDateFormat("dd/MM/yyyy");
o String str = format.format(d);
o S.o.p("Date : " + str );
o String str1 = "22/03/2009";
o Date d1 = format.parse(str1);
o S.o.p(d1);
 }
 Output
o String : 04/01/2010
o Sun Mar 22 00:00:00 IST 2009
www.SunilOS.com 18
Command line argument
 public class HelloName {
o public static void main(String[] args) {
o System.out.println("Hello " + args[0]);
o }
 }
 C:>java HelloName Vijay Dinanath Chauhan
 class args[0] args[1] args[2]
 C:>java HelloName “Vijay Dinanath” Chauhan
Disclaimer
This is an educational presentation to enhance the skill
of computer science students.
This presentation is available for free to computer
science students.
Some internet images from different URLs are used in
this presentation to simplify technical examples and
correlate examples with the real world.
We are grateful to owners of these URLs and pictures.
www.SunilOS.com 19
Thank You!
www.SunilOS.com 20
www.SunilOS.com

Initial Java Core Concept

  • 1.
    www.SunilOS.com 1 JAVA BASICS CoreConcepts www.sunilos.com www.raystec.com
  • 2.
    www.SunilOS.com 2 Java Programis a Class public class HelloJava { … } A class may contain multiple attributes (variables) and methods (functions). An executable class will have default ‘main’ method that is called by JVM to execute the program.
  • 3.
    www.SunilOS.com 3 My FirstProgram - HelloJava public class HelloJava { o public static void main(String[] args) { o System.out.println("Hello Java”); o } } public, class, static, and void are keywords. Keywords are always written in small letters. System.out object represents console
  • 4.
    www.SunilOS.com 4 Define avariable public class HelloJava { o public static void main(String[] args) { o String name = “Vijay”; o System.out.println(name); o } }
  • 5.
    www.SunilOS.com 5 Data Types int long byte short float double 1 2 4 8 4 8 -128,+127 -9.223E18, +9.223E18 -32768, +32767 -2147483648, +2147483647 +3.4 E+38 +1.7 E+308 Type Size Byte Range char 2 0, 65535 boolean 1 true, false 0 0 0 0 0 0 0 false Default
  • 6.
    www.SunilOS.com 6 Add twonumbers public class Add {  public static void main(String[] args) { o int a = 5; o int b = 10; o int sum = a + b; o System.out.println("Sum is " + sum);  } }
  • 7.
    www.SunilOS.com 7 Can Ibuy Pizza ? public class Add{  public static void main(String[] args) { o int rs = 110; o if( rs > 100 ){ o System.out.println(“Wow! I can buy Pizza ”); o }else{ o System.out.println(“Oh! I can not buy Pizza ”); o } o } }
  • 8.
    www.SunilOS.com 8 Other operators () ++ -- ! ~ instanceof * / % + - << >> >>> < > <= >= == != & ^ | && || ?: = op=
  • 9.
    www.SunilOS.com 9 Print Hello5 times - for public class HelloFor { public static void main(String[] args) { o for (int i = 0; i < 5; i++) {  System.out.println(i + "Hello"); o } o } }
  • 10.
    www.SunilOS.com 10 Print Hello5 times – while loop public class HelloWhile { public static void main(String[] args) { o int i = 0; o while (i < 5) {  System.out.println(i + "Hello");  i++; o } } }
  • 11.
    www.SunilOS.com 11 Print Hello5 times – do-while  public class HelloDoWhile {  public static void main(String[] args) { o int i = 0; o do {  System.out.println( i+ " Hello");  i++; o } while (i < 5);  }  }
  • 12.
    www.SunilOS.com 12 10 List ofelements a.k.a. Array 20 [0] 18 .. 10 8 6 4 2 [1] [8] [9] [2] [3] [4] [n] length int[] list = new int[10]; list[0] =2; list[1] =4; …. Or int[] list = {2,4,6,8,10,12,14,16,18,20};
  • 13.
    www.SunilOS.com 13 Foreach statement publicclass HelloFor { public static void main(String[] args) { o int[] list={ 2, 4, 6, 8, 10}; o System.out.println(“Length: ” + list.length); o for (int element : list) {  System.out.println(element); o } o } }
  • 14.
    www.SunilOS.com 14 Handle aString  String name = "Vijay Dinanath Chouhan";  S.o.p(" String Length- " + name.length());  S.o.p(" 7th character is- " + name.charAt(6));  S.o.p(" Dina index is- " + name.indexOf("Dina"));  S.o.p(" First i Position- " + name.indexOf("i"));  S.o.p(" Last i Position- " + name.lastIndexOf("i"));  S.o.p(" a is replaced by b- " + name.replace("a", "b"));  S.o.p(" All a is replaced by b- “ + name.replaceAll("a", "b"));  S.o.p(“ Chhota vijay- " + name.toLowerCase());  S.o.p(" Bada vijay- " + name.toUpperCase());  S.o.p(" Starts With Vijay- " + name.startsWith("Vijay"));  S.o.p(" Ends with han- " + name.endsWith("han"));  S.o.p(" Substring- " + name.substring(6)); Note : S.o.p = System.out.println
  • 15.
    www.SunilOS.com 15 Mathematical operations public static void main(String[] args) { o S.o.p(“ Mathematics functions"); o S.o.p(" Max 2,5 - " + Math.max(2,5)); o S.o.p(" Min 2,5 - " + Math.min(2,5)); o S.o.p(" Absolute 3.7 - " + Math.abs(3.7)); o S.o.p(" Exp 10 - " + Math.exp(10)); o S.o.p(" Random Number- " + Math.random()); o S.o.p(" Square Root- " + Math.sqrt(4));  }  Note : S.o.p = System.out.println
  • 16.
    www.SunilOS.com 16 Handle adate  import java.util.*;  public class TestDate {  public static void main(String[] args) { o Date d = new Date(); o S.o.p("Date : " +d); o S.o.p ("Long Time : " +d.getTime());  }  Output o Date : Mon Jan 04 00:35:53 IST 2010 o Long Time : 1262545553156
  • 17.
    www.SunilOS.com 17 Format aDate  import java.util.*; import java.text.SimpleDateFormat;  public class TestDateFormat{  public static void main(String[] args) { o Date d = new Date(); o SimpleDateFormat format= new SimpleDateFormat("dd/MM/yyyy"); o String str = format.format(d); o S.o.p("Date : " + str ); o String str1 = "22/03/2009"; o Date d1 = format.parse(str1); o S.o.p(d1);  }  Output o String : 04/01/2010 o Sun Mar 22 00:00:00 IST 2009
  • 18.
    www.SunilOS.com 18 Command lineargument  public class HelloName { o public static void main(String[] args) { o System.out.println("Hello " + args[0]); o }  }  C:>java HelloName Vijay Dinanath Chauhan  class args[0] args[1] args[2]  C:>java HelloName “Vijay Dinanath” Chauhan
  • 19.
    Disclaimer This is aneducational presentation to enhance the skill of computer science students. This presentation is available for free to computer science students. Some internet images from different URLs are used in this presentation to simplify technical examples and correlate examples with the real world. We are grateful to owners of these URLs and pictures. www.SunilOS.com 19
  • 20.

Editor's Notes