2. Introduction
• general depiction of a java class using a
programming environment such as eclipse :
• //Package
• //import directives
• Access-Modifiers class className{
• // variables
• // Method ( functions) definitions
• }
3. Simple example
public class tom {
public static void main(String[] args) {
System.out.println("simple");
}}
• Why static ? static is keyword indicating the
method is static which means it has static address
which must determined at compile time
4. Questions for the previous example
• What main mean ? keyword which means an
address where the application or program will
start executing
What the output ??
System.out.println("abt"hello"");
The output abd "hello"
5. What the output ??
public class Mainx {
public static void main(String[] args) {
int x=3;
int y=4;
System.out.println(x+y);
}
}
Output 7
6. functions
• public class MyClass{
• public static void main(String[] args) {
• funA(); // direct function call statement
• System.out.print("bye");
• }
• static void funA(){
• //function body begin
• System.out.print("No value to return");
• }//function body ends
• }
7. Q for previous ex
• Questions :
• 1- What is the function name?
• 2- What is the function header?
• 4- Why the function does not have a return
statement?
• Answers :
• 1- FunA
• 2- Static void funA()
• 4- Because the function return data type field is
void, therefore it return
8. Overloading
• public class Mainx {
• public static void main(String[] args) {
• System.out.print(Fun(2.00));
• }
• static int fun(int x){
• return 1;
• }
• static int fun(double x){
• return 1;
• }
• static int fun(int x, int y){
• return 1;
• }
• }
9. Visibility
• public class tom {
• public static void main(String[] args) {
• {
• char c='a';
• }
• System.out.println(c);//error c not visible
• }
• }
• In can see the out but out cant see the in
10. visiblity
• static int Fun(int x){
• int z=1;
• {
• int x=4;// Naming collision error, x is already
declared
• }
• return 0;
• }
11. Recursive functions
• Example 10: Finding the factorial of an integer by using recursive function
calls
• public class Mainx {
• public static void main(String[] args) {
• int z=4;
• System.out.println(factorial(z));
• }
• static int factorial(int x){
• if(x==0)
• return 1;
• else
• return x*factorial(x-1);
• }
• }
12. Arrays
• One dimensional array
• public class Mainx {
• public static void main(String[] args) {
• int a[]={1,2,3};
• for(int i=0;i<3;i++){
• System.out.print(a[i]);
• }
• }
• }
14. Find the error
• public class MyClass {
• public static void main(String[] args) {
• int x=5;
• {
• int x=6;
• }
• System.out.println(x);
• }
• }
15. Find the error
• public class MyClass {
• public static void main(String[] args) {
• int x=fun();
• }
• public int fun(){
• System.out.println("hello");
• return 1;
• }
• }
16. Find the error
• public class MyClass {
• public static void main(String[] args) {
• int x=1;
• if(x>=1){
• int y=3;
• }
• System.out.println(x+y);
• }
• }
17. Find the error
• public class MyClass {
• public static void main(String[] args) {
• fun(3);
• }
• static void fun(int z){
• int z=3;
• }
• }
18. What the output
• public class MyClass {
• public static void main(String[] args) {
• int a[]={1,2,3};
• System.out.println(fun(a));
• }
• public static int fun(int a[]){
• int sum=0;
• for(int i=0;i<3;i++){
• sum+=a[i];
• }
• return sum;
• }
• }
19. What the output
• public class tom {
• public static int fun(int x){
• return ++x;
• }
• public static void main(String[] args) {
• int z=4;
• z=fun(fun(fun(z)));
• System.out.println(z);
• }
• }
20. Find the error
• public class tom {
• public static int fun(int x){
• return x;
• }
• public static void main(String[] args) {
• double z=4;
• z=fun(fun(fun(z)));
• System.out.println(z);
• }
• }
21. Class variable
• public class tom {
• static int z=2;
• public static void fun2(){
• System.out.println(++z);
• }
• public static void fun3(){
• System.out.println(++z);
• }
• public static void main(String[] args) {
• fun3(); fun2();
• }
• }
22. What the output
• public class tom {
• static int z=8;
• public static void fun2(){
• System.out.println(++z);
• }
• public static void fun3(){
• System.out.println(++z);
• }
• public static void main(String[] args) {
• fun3(); fun2();fun3(); fun2();
• }
• }
23. What the output
• public class MyClass {
• static int z=5;
• public static void fun1(int x){
• int z=4,y=5;
System.out.println(x+y+z);
• }
• public static void fun2(int k){
• int x=1, y=2;
• System.out.println(x+y+k+z);
• }
• public static void main(String[] args) { fun1(1);fun2(2); } }