Get ready to have fun in learning JAVA

shalini_goel14
@shalini-goel14-ASmC2J β€’ Oct 26, 2024

Are you afraid of Java? No need to be anymore because now you can have "Fun with Java" in this thread. πŸ˜€

After seeing very less no of Java programmers here, I thought of starting this thread. Hope it will not become dead and will benefit most of the CEans here.

The purpose of this thread is to:
Teach Java to everyone (CEans of different streams other than Computers or IT also) and make more and more CEans active here.

How this thread will work:
1. In this thread Java language will be taught and at the end of every lecture,assignments will be given.
2. Next lecture will start only if given assignments will be completed by CEans. If in any case they could not do so, they can reply back as "No" or can show/ask the problems.
3. References will be taken from "Complete Reference in Java J2SE5" by 'Herbert Schildt' ,"SCJP1.5 book" by 'Kaithy Seirra and Bert Bates' and trainer's learnings.

Very Important : Kindly keep following things in mind while posting further in this thread:
1. In any case- "Usage of sms text and languages other than English and Java will not be tolerated".
2. Off the topic discussions will not be allowed here.
3. No hesitation should be there in asking any questions.People here will be free to ask as dumb/stupid question as they can ask rather than not asking even a single one.
4. Assignment will consist of writing a Java program. (Initially starting from very simpler ones.)
5. Java Programmers other than trainer here are most welcome to share their knowledge or correct the trainer at any point of time.
6. Everyone is free to give assignments here.
7. Assignments are expected to be completed by end of every day so that learning process can be speed up.
8. Rules 1 to 7 are vapplicable to CE Admins/CE Mods/CE Editors/CE Ambassadors.

[Note: It will be really very pleasing if all points 1 to 8 mentioned above are followed by everyone here. 😁 ]

So kindly wait for Day 1 class which is going to start soon. πŸ˜‰
______________________________________________________

You all can follow following list of contents showing quick links for topics discussed so far in this thread.

Day1
Java Overview:
#-Link-Snipped-#
Setting up Java Environment in your machine:
#-Link-Snipped-#
Writing a simple Java Program
#-Link-Snipped-#

Day2
Identifiers and Declaration Rules
#-Link-Snipped-#
Variable Declarations
#-Link-Snipped-#

Day3
Control Statements
#-Link-Snipped-#
Iteration Statements
#-Link-Snipped-#
Break and Continue Statements
#-Link-Snipped-#

Day4
Array Declaration and Construction
#-Link-Snipped-#
Array Initialization
#-Link-Snipped-#

Day5
Introduction to classes
#-Link-Snipped-#
Declaration of objects and assignment of object reference variables
#-Link-Snipped-#

Day 6
Using Methods
#-Link-Snipped-#
Using Constructors
#-Link-Snipped-#

Day 7
Overloading Methods
#-Link-Snipped-#

Special Classes
Operators
#-Link-Snipped-#
Type Conversion and Type Casting
#-Link-Snipped-#

*****
How to overcome Operator overloading in Java
#-Link-Snipped-#
How to confirm whether JDK is installed in your system or not
#-Link-Snipped-#
Command line arguments
#-Link-Snipped-#
Java Keywords
#-Link-Snipped-#
Size and range of Java primitive types
#-Link-Snipped-#
'this' keyword
#-Link-Snipped-#

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • safwan

    @safwan-NH7W5Y Feb 24, 2009

    hay i am not getting jdk1.6 where to go even i searched it on googel but not precise link is not there.
    and i am back after exam . now i started learning java.

  • komputergeek

    @komputergeek-Yf5hJ4 Feb 24, 2009

    @safwan : Check this
    #-Link-Snipped-#

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 25, 2009

    ok I am very depressed with the responses I am getting here from you guys, especially from Day 3 class.πŸ˜”

    Anyways Day 4 class is going to start soon. Hope you people will show some interest in it. I am doing it for you people only not for me.

  • komputergeek

    @komputergeek-Yf5hJ4 Feb 25, 2009

    @shalini :This thread is definitely interesting but in my opinion,we should not wait for assignments at this difficulty level.You should increase speed of teaching.

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 25, 2009

    komputergeekYou should increase speed of teaching.

    Good suggestion komputergeek,

    Even I also want to do so but cannot find time. It takes time for me to create those lectures referring different books and the biggest problem with me is the text editor here which has very limited functionalities. πŸ˜”

  • silverscorpion

    @silverscorpion-iJKtdQ Feb 25, 2009

    Yep, I thought of exactly the same thing.
    Things will automatically get sorted out as classes progress. I'm waiting for the oops concepts here.
    Dont get me wrong here, but I really dont like writing programs to find sum of two numbers.Well, I can hear you shouting, but that's it.
    As the class progresses to more tougher concepts, I assure you I'll participate more, as only then can I effectively learn by writing programs.
    Dont get angry for this, now..chill.

  • silverscorpion

    @silverscorpion-iJKtdQ Feb 25, 2009

    oops, seems like you have already replied to komputergeek's opinion.. I didnt see that..

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 25, 2009

    [Day 4-Class ]

    This class will include "Array Declaration, Construction and Intialization"

    How to declare an array

    a) array of primitives
    int[] intArray; (more recommended)
    or
    int intArray[]; ->avoid using this form.

    b) array of object references
    Suppose you have a your own made class Employee or an inbuilt Java class like Thread, Long, Integer etc. Array declaration will be like
    Employee[] employeeArray;
    Thread[] threadsArray;
    Long[] longArray;
    Integer[] intArray;
    or
    Employee employeeArray[];
    Thread threadsArray [];
    Long longArray [];
    Integer intArray [];

    c) Multidimensional arrays declaration(arrays of arays)
    String[] [] [] string3DArray;
    String[] string2DArray;

    Constructing an array

    Constructing array means creating the array object on the heap(where all objects live). So to create an array object Java must know how much space to allocate on the heap for it, so we need to specify the size of the array at creation time.

    A) Constructing One-Dimensional Arrays

    new is a java keyword(more about it in later classes) but here it works like a special operator that allocates memory.

    So now how will I construct a 1-D array:
    Step 1: First I will declare it like told above.
    Syntax: type var-name;
    int[] intArray; //it simply declares the array of int
    Step2: Next I need to allocate memory for arrays using new keyword
    Syntax: var-name = new type[size] ;
    intArray = new int[4];//constructs an array and assigns it the intArray variable.

    Now see what happens behind the scenes :
    Below is the figure showing A one-dimensional array on the heap.

    [​IMG]

    So line step2 puts one new object on the heap (name intArray) holding 4 elements of int type each with default values as 0.

    We can also declare and construct an array in one sentence:

    int[] intArray = new int[4];

    [ Qn: How many objects are created on the heap by following line:
    Thread[] threadsArray = new Thread [5];
    Qn: Will the following line Compile? Give reason also.
    int[] intArray =new int[] ;
    ]

    Constructing multi-dimensional Array
    Multi-dimensional arrays are simple arrays of arrays. So a 2D array of type int is really an object of type int array(int []) with each element in that array holding a reference to another int array.The second dimension actually holds the actual int primitives.
    int[][] int2DArray= new int[3][];

    Note that only first brackets are given a size. This is acceptable in Java since the JVM needs Following picture demonstrates this example:

    int[] [] int2DArray =new int[3][];
    int2DArray[0]= new int[2];
    int2DArray[0][0]=6;
    int2DArray[0][1]=7;
    int2DArray[0]= new int[3];
    int2DArray[0][0]=9;
    int2DArray[0][0]=8;
    int2DArray[0][0]=5;
    [​IMG]

    [PS I: Day4-class next part will include intializing an array.You all need to give me some time. Please do answer the two questions asked here by that time. Feel free to ask your doubts.
    PS II :I wonder how you people are so clear about what I taught you till now. I think I need to ask all my doubts to you guys. Right komputergeek and Scorpion?πŸ˜‰
    ]

  • silverscorpion

    @silverscorpion-iJKtdQ Feb 26, 2009

    shalini_goel14intArray = new intArray[4];//constructs an array and assigns it the intArray variable.

    this should be intArray=new int[4], right?

  • silverscorpion

    @silverscorpion-iJKtdQ Feb 26, 2009

    shalini_goel14PS II :I wonder how you people are so clear about what I taught you till now. I think I need to ask all my doubts to you guys. Right komputergeek and Scorpion?πŸ˜‰
    ]

    well, you might as well be right. you can try asking us your doubts in the fundamentals, because, you would always be immersed in advanced concepts, and on the other hand, I never moved beyond the fundamentals in java..😁

  • silverscorpion

    @silverscorpion-iJKtdQ Feb 26, 2009

    shalini_goel14[ Qn: How many objects are created on the heap by following line:
    Thread[] threadsArray = new Thread [5];
    Qn: Will the following line Compile? Give reason also.
    int[] intArray =new int[] ;
    ]
    ]

    well, for the first question, only one object is created in the heap, because it's a single dimensional array.

    as for the second line, it will not compile, because, the size of the array is not specified and the compiler will not know how much space to allocate for that.

    Are these answers correct?😁

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 26, 2009

    Yes absolutely correct Scorpion 😁. Good

    One more question for you all, What will be your answer if I make
    Thread[] threadsArray = new Thread [5];
    a 2-D array. How many objects will be created now?

    [Qn: How many objects will be created on the heap by following line:
    Thread[] threadsArray = new Thread [5];
    ]

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 26, 2009

    [Day4 -Class ..Contd ]

    Initializing primitive data types array:
    int[] intArray =new int[5];
    This step only creates a reference variable intArray and allocates space of 5 elements in the array with default values as 0.
    [ Qn Write a program for putting values in a 1-D Array and then printing them on console.]

    Initializing non-primitive data types array:

    Array[] pets=new animal[3];
    This creates one array object on the heap with 3 null references of type Animal (class) but we don’t have any Animal class objects, so next step is to create some Animal objects and assign them to index positions in the array referenced by variable pets
    pets[0]=new Animal ();
    pets[1]=new Animal ();
    pets[3]=new Animal ();
    So now above 3 lines put 3 new Animal objects on the heap ad assigns them to the 3 index positions (elements) in the pets array.
    [ Qn Write a program for putting values in a 2-D Array and then printing them on console.]


    Initializing a 2-D Array:
    int[][] scores =new int[3][]; //Declare and create an array holding 3 references to int arrays
    scores[0]=new int[4]; //the first element in the scores array is an int array of four int elements
    scores[1]=new int[6]; //The second element in the scores array is an int array of 6 elements
    scores[2]=new int[1];//The third element in the scores array is an int array of one int element.


    Declaring, constructing and initializing on one line:
    Following lines of code are legal to use in Java
    A)int[] dots={6,5,8}
    [Note: In above line, no size limit is mentioned. Size is determined by the comma-separated items in the curly braces. ]
    B)

    int x=5;
    int[] dots={6, x, 8}

    Above two lines of code is also legal.

    C)

    Dog dogObj= new Dof(β€œFrodo”);
    Dog[] myDogs={ dogObj, new Dog(β€œClover”), new Dog(β€œAiko”) };

    In above piece of code, in total 4 objects are created
    1 Dog object referenced by dogObj and by myDogs[0]
    1 Dog[] array object referenced by myDogs
    2 Dog objects referenced by mydogs[1] and myDogs[2]


    D)Similarly for multi-Dimensional arrays:

    int[] scores ={ {5,2,4,7}, {9,2}, {3,4} );

    It creates total of 4 objects on the heap.
    1 object of int array type referenced by scores[0]
    1 object of int array type referenced by scores[1]
    1 object of int array type referenced by scores[2]
    1 object of array of int arrays type referenced by scores

    In actual what happens behind the scenes:
    First an array of int array is constructed (scores as reference variable to it). The scores array has a length of three (as found from items in curly braces). Each of the 3 elements in the scores array is a reference variable to an int array, so the 3 int arrays are constructed and assigned to the 3 elements in the scores array.
    The size of each of the int arrays is derived from the no. of items within the corresponding inner curly braces.
    First array has length of four {5,2,4,7}
    Second array has length of two{9,2 }
    Second array has length of two{3,4 }
    So in all we have 4 objects, 1 array object referencing int arrays and 3 int arrays each initialized with actual int values

    See below a sample 1-D Array program:

    package myjava;
    
    /**
     *
     * @author shalinig
     */
    public class ArrayExample {
        public static void main(String[] args){
            
            /*Declaring, constructing and intializing 1-D 
             array in a single line
             */
            int[] nums={5, 6, 7, 8, 9};
            
            /* nums.length gives the length of array
             (in this case it is 5
             */
            for(int i=0; i<nums.length;i++){
                //nums[i] gives the array element at ith position
                System.out.println("Element at index "+i+" = "+nums[i]);
            }
        }
    
    }
    

    Ouput is:

    Element at index 0 = 5
    Element at index 1 = 6
    Element at index 2 = 7
    Element at index 3 = 8
    Element at index 4 = 9

    [Qn Write down the default values for each of the following variable types :
    a)Object reference
    b)Byte
    c)Short
    d)Int
    e)Long
    f)Float
    g)Double
    h)Boolean
    i)Char
    ]
    PS I: Now with this Arrays in Java is finished in Day4-Class. Few difficult things in thsi topic are still left but I cannot discuss them without classes.So they will be taught later. If anyone has any doubt till now or feels I have missed something important thing, Please do let me know.πŸ˜€

    PS II: I will soon start Classes part. Just give me some time.

    Thanks

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 26, 2009

    silverscorpionthis should be intArray=new int[4], right?

    Yes you are right Scorpion. I am correcting it.

    Thanks for correcting me everytime. πŸ˜€

  • arunhero99

    @arunhero99-LRZv4M Feb 26, 2009

    wow..his has become a full fledged training room...kudos shailini....gr8 job

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 26, 2009

    arunhero99wow..his has become a full fledged training room...kudos shailini....gr8 job

    Thanks Sir. 😁

    PS: SMS language is not allowed here. Please read my first post in this thread.πŸ˜€ Better correct it.

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 27, 2009

    [Day5 -Class]

    Introduction to Classes

    Class is nothing bur is a template for an object and object can be nay real worl object. Let us say Dog is a real world thing and it can be of different types or can have diferent names. So Dog can be made as a class and then we can instantiate that class to make an object which will have its own properties or attributes Eg. Dog is a class and Dog with name "Frodo" is 1 object and Dog with name "Aiko" is another object.
    So overall it is said
    "Class is a template for an object and an object is an instance of a class."
    Both are used interchangeably.

    Class Definition:
    General form of a class definition is:

    class classname{

    type instance-variable1;
    type instance-variable2;
    ..........
    type instance-variableN;

    type methodname1(parameter list){
    //body of method
    }

    type methodname2(parameter list){
    //body of method
    }

    .............
    type methodname1(parameter list){
    //body of method
    }
    }

    instance variable: The data, or variables , defined within a class are called instance-variables.
    Methods: The actual code is contined within them.

    Collectively, instance-variables and methods are called as clas members of a class.

    Instance variables are called so because each instance(or object of the class) contains its own copy of these variables. Thus the data for one object is separate and unique from the data for another.
    Static variables are just opposite of instance variables. They share the same data for all objects or instances of a class.

    Java classes need not to have a main method.

    A simple clas Example

    package myjava;
    
    import java.util.Date;
    
    /**
     *
     * @author shalinig
     */
    class CrazyEngineer{
    
    String memberName;
    String engineeringTrade;
    String location;
    }
    

    The above class does not have any methods but yes has instance variables.

    A class defines a new data type so here CrazyEngineer is a new datatype. So now we can use this CrazyEngineer datatype to declare objects. A class do not create an object in actual, we need to create it using new operator.

    So to actually create a CrazyEngineer object, we will use the following statement:

    /* creates a CrazyEngineer object called crazyEngineerObj */
    CrazyEngineer crazyEngineerObj = new CrazyEngineer();

    After this line executes crazyEngineerObj will be an instance of CrazyEngineer

    Thus every CrazyEngineer object will have its own copies of instance variables memberName, engineeringTrade and location. To access these variables a dot(.) operator is used. It links the name of the object with the name of the instance variable. Dot(.) operator is also used to access methods contained within a class.

    e.g. To assign value="shalini_goel14" to memberName, following statement will be used:

    crazyEngineerObj.memberName="shalini_goel14" ;

    The above line tells the compiler to assign the copy of memberName that is contained within the crazyEngineerObj object the value "shalini_goel14"

    A sample class program using dot operator:

    package myjava;
    
    import java.util.Date;
    
    /**
     *
     * @author shalinig
     */
    class CrazyEngineer{
    
    String memberName;
    String engineeringTrade;
    String location;
    }
    
    public class CrazyEngineerDemo{
    
        public static void main(String[] args){
         CrazyEngineer crazyEngineerObj =new CrazyEngineer();
         StringBuilder profileInOneLine=null;
         
         //assigns values to crazyEngineerObj's instance variables
         crazyEngineerObj.memberName="shalini_goel14";
         crazyEngineerObj.engineeringTrade="Computer Science";
         crazyEngineerObj.location="Bangalore";
    
         
         //write profile in one line
         profileInOneLine = new StringBuilder(crazyEngineerObj.memberName+" is a crazy ");
         profileInOneLine.append(crazyEngineerObj.engineeringTrade+" engineer");
         profileInOneLine.append(" located in "+crazyEngineerObj.location+";");
    
         //prints the profile in one line
         System.out.println("Profile is:::"+profileInOneLine);    
         
        }
        
    }
    

    Output is:

    Profile is:::shalini_goel14 is a crazy Computer Science engineer located in Bangalore;

    [Qn Write the same program as shown above but declare two CrazyEngineer objects and the print them as done in above program only]

    PS: Day5 class next part will include objects declaration and behind the scenes happenings but need to wait for it. πŸ˜€

  • Yamini L

    @yamini-uMEVDQ Feb 27, 2009

    First i would like to congratulate you for all your efforts shalini..Good job..

    Now my answer for your assignment question
    Qn:Write a program to store values in a 1 d array and print them in the console.


    import java.io.*;
    import java.lang.*;
    class arr
    {
    public static void main(String args[])
    {
    //creating and initializing arrays
    int[] a={1,4,6,8,10};
    for(int i=1;i<a.length;i++)
    {
    System.out.print("value at position"+i);
    System.out.println(" "+a);
    }
    }
    }


    __________________

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 27, 2009

    Thanks for good words miniy😁

    PS: Please enclose your code within code tags(use icon # shown above) and Please try to make class name with intial capital letter e.g 'Arr' instead of 'arr'

    Thanks

  • Yamini L

    @yamini-uMEVDQ Feb 27, 2009

    [Qn Write the same program as shown above but declare two CrazyEngineer objects and the print them as done in above program only]

    Now i am doing the most recent assignment you had given....Hope it works..

    import java.io.*;
    import java.lang.*;
    import java.util.*;
    class CrazyEngineer{
    String memberName;
    String engineeringTrade;
    String location;
    }
    public class CrazyEngineerDemo{
    public static void main(String[] args){
    CrazyEngineer crazyEngineerObj =new CrazyEngineer();
    CrazyEngineer crazyEngineerObj1=new CrazyEngineer();//second object
    StringBuilder profileInOneLine=null;

    //assigns values to crazyEngineerObj's instance variables
    crazyEngineerObj.memberName="shalini_goel14";
    crazyEngineerObj.engineeringTrade="Computer Science";
    crazyEngineerObj.location="Bangalore";
    //assigns values to crazyEngineerobj1's instance variables
    crazyEngineerObj1.memberName="miniy";
    crazyEngineerObj1.engineeringTrade="Computer Science";
    crazyEngineerObj1.location="India";


    //write profile in one line
    profileInOneLine = new StringBuilder(crazyEngineerObj.memberName+" is a crazy ");
    profileInOneLine.append(crazyEngineerObj.engineeringTrade+" engineer");
    profileInOneLine.append(" located in "+crazyEngineerObj.location+";");
    //prints the profile in one line
    System.out.println("Profile is:::"+profileInOneLine);
    //second profile
    profileInOneLine = new StringBuilder(crazyEngineerObj1.memberName+" is a crazy ");
    profileInOneLine.append(crazyEngineerObj1.engineeringTrade+" engineer");
    profileInOneLine.append(" located in "+crazyEngineerObj1.location+";");
    //prints the profile in one line
    System.out.println("Profile is:::"+profileInOneLine);
    }

    }
  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 27, 2009

    miniy[Qn Write the same program as shown above but declare two CrazyEngineer objects and the print them as done in above program only]

    Now i am doing the most recent assignment you had given....Hope it works..

    What does that mean "Hope it works". Man, write well compiled and executed programs only. Don't do copy paste in one only ok.πŸ˜€ Well Good keep it up. Rest of the questions are also waiting for you.

  • Yamini L

    @yamini-uMEVDQ Feb 27, 2009

    [Qn Write down the default values for each of the following variable types :
    a)Object reference
    b)Byte
    c)Short
    d)Int
    e)Long
    f)Float
    g)Double
    h)Boolean
    i)Char
    ]


    Object Reference-> NULL
    Byte -> 0
    Short -> 0
    Int -> 0
    Long -> 0L
    Float -> 0.0f
    Double -> 0.0d
    Boolean -> false
    Char -> '\u0000'

  • Yamini L

    @yamini-uMEVDQ Feb 27, 2009

    shalini_goel14What does that mean "Hope it works". Man, write well compiled and executed programs only. Don't do copy paste in one only ok.πŸ˜€ Well Good keep it up. Rest of the questions are also waiting for you.

    Hey you wanted it in the same format..Thats why i just included another object in it:smile:..I compiled it,got the output..anyways am also waiting for your forthcoming classes and assignments..πŸ‘

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 27, 2009

    miniyHey you wanted it in the same format..Thats why i just included another object in it:smile:..I compiled it,got the output..anyways am also waiting for your forthcoming classes and assignments..πŸ‘

    Perfect, miniy Please write the output also if it was giving output 😁

    PS: There are many more pending assignements for you. Complete them by the time I start new class. πŸ˜‰

  • Yamini L

    @yamini-uMEVDQ Feb 27, 2009

    [Qn Write the same program as shown above but declare two CrazyEngineer objects and the print them as done in above program only]

    Output

    Profile is:::shalini_goel14 is a crazy Computer Science engineer located in Banalore;
    Profile is:::miniy is a crazy Computer Science engineer located in India;

    πŸ‘

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 27, 2009

    Miny, good one. This was cheating 😁, you ate 'g' in Bangalore and made it 'Banalore' right? while your program is having 'g' at its corect place.

    Anyways leave it. Don't post anymore here for that same program. Devote time on other assignments ok. And from next time try to include output with program in same post only ok. It makes easier for others to understand ok. πŸ˜€

  • Yamini L

    @yamini-uMEVDQ Feb 27, 2009

    Last post for this assignment..Please do forgive me for this..Just like to justify..This was the actual output i got..While i tried to copy it from command prompt, i missed out 'g'πŸ˜”..Believe me😁..

    Profile is:::shalini_goel14 is a crazy Computer Science engineer located in Bang
    alore;
    Profile is:::miniy is a crazy Computer Science engineer located in India;

    Now onwards,i would be shifting my focus on to other assignments..πŸ‘

  • Yamini L

    @yamini-uMEVDQ Feb 27, 2009

    [ Qn Write a program for putting values in a 2-D Array and then printing them on console.]

    Code

    import java.io.*;
    class Arrtwod
    {
    public static void main(String args[])
    {
    int[][] a={              {1,2,3}, 
                                {4,5,6},
                                {7,8,9},
                                {10,11,12}};  //Creating and initializing 2-D Array
    for(int i=0;i<4;i++)
    {
    for(int j=0;j<3;j++)
    {
    System.out.println("Value at a["+i+","+j+"] = "+a[i][j]); //printing the values at console
    }
    }
    }
    }
    

    Output

    Value at a[0,0] = 1
    Value at a[0,1] = 2
    Value at a[0,2] = 3
    Value at a[1,0] = 4
    Value at a[1,1] = 5
    Value at a[1,2] = 6
    Value at a[2,0] = 7
    Value at a[2,1] = 8
    Value at a[2,2] = 9
    Value at a[3,0] = 10
    Value at a[3,1] = 11
    Value at a[3,2] = 12
  • ms_cs

    @ms-cs-Ab8svl Feb 27, 2009

    I have one doubt.Whether goto statement is avilable in java?

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 27, 2009

    ms_csI have one doubt.Whether goto statement is avilable in java?

    In the Java(tm) programming language, goto is a reserved word; the Java programming language does not have a goto statement. However there are alternative statements that you can use in the Java programming language in place of the goto statement.(ue break and continue to avoid use of goto )

    ms_cs, I woould again say to you- "Don't try to use complicated features of C/C++ in Java" Java provide many better things to avoid those complicated faetures. πŸ˜€ Keep C/C++ aside while learning Java , that would be better.

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 27, 2009

    [Day5-Class ..contd]

    Declaring Objects

    Declaring objects for a new datatype(which is actually of defined class type) is a 2-step process:

    Step1: We must declare a variable of class type. Considering the example of class CrazyEngineer

    CrazyEngineer crazyEngineerObj;

    It declares a reference to object. Note, object is not created in actual. It is simply a variable that can refer to an object. After execution of above line , object is assigned default value that is null. So crazyEngineerObj=null right now.

    Step2: Now we need to acquire an actual physical copy of the object and assign it to that variable. This is done using new operator.

    crazyEngineerObj = new CrazyEngineer();

    It allocates a CrazyEngineer object. Now crazyEngineerObj is no more null.

    Now what this new operator does in actual?
    It dynamically allocates (that is allocates at runtime) memory for an object and returns a reference to it. This reference is actually the address of the object in memory allocated by new. This reference is then stored in the
    variable (here like crazyEngineerObj)

    All above things will become more clear to you with following diagram.
    [​IMG]

    Assigning Object Reference variables

    Anyone any ideas what happens inside when following two lines execute?

    CrazyEngineer crazyEngineer1=new CrazyEngineer ();
    CrazyEngineer crazyEngineer2= crazyEngineer1; //assigns a copy of the crazyEngineer1 object to crazyEngineer2.

    As we all know now in first statement crazyEngineer1 object is created and
    and allocated memory also but what about crazyEngineer2 object?Is is also allocated space in memory? The answer is no.

    See the situation with the help of following fig:

    [​IMG]

    Actually what happens, there remains only one object which was created usin new operator but there are two reference variables that refer to single CrazyEngineer object.Those variables are crazyEngineer1 and crazyEngineer2. So simply crazyEngineer2 refers to the same object as does crazyEngineer1. Thus any changes made to the object through crazyEngineer2 will affect the object which crazyEngineer1 is referring, since they both refer to the same object.

    One more important thing is even if object crazyEngineer1 is made to refer any other object or is assigned value null , then in any case it will not affect crazyEngineer2. crazyEngineer2 will keep on referrring to the same CrazyEngineer object. It will not become null even if crazyEngineer1 is made null

    CrazyEngineer crazyEngineer1= new CrazyEngineer();
    CrazyEngineer crazyEngineer2= crazyEngineer1;
    ...
    crazyEngineer1=null;

    Look, though crazyEngineer1 is made as null but cazyEngneer2 will keep on pointing to the original object.

    Though situation is somewaht different in case of assignment of local primitives. Will discuss it separately more.

    PS: Any questions till now , Please feel free to ask and do complete the assignments. I need to give many more questions to you all. πŸ˜€

  • Raviteja.g

    @ravitejag-02nJVr Feb 27, 2009

    i have doubt about operators
    can i ask now because i didn't have completed the assignments because of lack of time but still trying to adjust

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 27, 2009

    Raviteja.gi have doubt about operators
    can i ask now because i didn't have completed the assignments because of lack of time but still trying to adjust

    Sure, there is no boundations of doing assignments and asking questions here. πŸ˜€ Take your own time but just try to not to make all my efforts in vain. πŸ˜”

  • Raviteja.g

    @ravitejag-02nJVr Feb 28, 2009

    its ok mam
    and my doubt is about operator overloading in matrices
    i was asked to design a java program explaining operator overlaoding
    for which i have designed a program that will explain the function of +(operator)
    but they expected in matrices which i don't know

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 28, 2009

    Raviteja.gits ok mam
    and my doubt is about operator overloading in matrices
    i was asked to design a java program explaining operator overlaoding
    for which i have designed a program that will explain the function of +(operator)
    but they expected in matrices which i don't know

    Very good question but can you first put some more light on what exactly is operator overloading?-actually I don't know C/C++ πŸ˜”

    Then I can answer your question.πŸ˜€

  • Raviteja.g

    @ravitejag-02nJVr Feb 28, 2009

    Operator overloading means the work the same operator is different in different situations
    for example
    case 1:

    a+b

    where a,b are two int variables and the +(operator) is used for addition here
    case 2:

    ravi+teja

    where ravi,teja are two strings
    now comes the point
    here the same +(operator) was used but here it concatenates the two strings and gives
    raviteja as output

    i don't remember the syntax for second one correct me if i'm wrong
    i think you understand now what is operator overloading
    It is a specific case of polymorphism in which some or all of operators<a href="https://en.wikipedia.org/wiki/Operator_%28programming%29" target="_blank" rel="nofollow noopener noreferrer">Operator %28Programming%29</a> like +, =, or == have different implementations depending on the types of their arguments.

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 28, 2009

    Man, I love Java. It provides such a good feature that you don't need to even think about your old C++ operator overloading.πŸ˜€

    String class in Java takes care of your problem. It will add integer values but append string values.

    If you add
    int + int = output is always int value
    int +String = int will be appended to String value (int is automatically converted to String value)
    String +String = again appended String

    Look at the following example

    package myjava;
    
    /**
     *
     * @author shalinig
     */
    public class OperatorOverloadingInJava {
    
        public static void main(String[] args){
            
            int integerVal1=10;
            int integerVal2=20;
            
            String stringVal1="Shalini";
            String stringVal2="Goel";
            
            System.out.println("(integerVal1 + integerVal2)= "+(integerVal1+integerVal2));
            System.out.println("integerVal1 + integerVal2 = "+integerVal1+integerVal2);
            System.out.println("stringVal1 + stringVal2= "+stringVal1+stringVal2);
        }
    }
    

    Output:

    (integerVal1 + integerVal2)= 30
    integerVal1 + integerVal2 = 1020
    stringVal1 + stringVal2= ShaliniGoel

    Hope I have cleared your doubt πŸ˜€

  • Raviteja.g

    @ravitejag-02nJVr Feb 28, 2009

    oops!
    you misunderstood.
    i'm talking operator overloading in java i too don't know about c++
    but my problem is how can operator overloding is done in matrices.

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 28, 2009

    Raviteja.goops!
    you misunderstood.
    i'm talking operator overloading in java i too don't know about c++
    but my problem is how can operator overloding is done in matrices.

    Raviteja,
    I showed you an example right? There is no need of any operator overloading concept here right?

    In my shown example , you see that same "+" operator is adding two integers also as well as appending two Strings also(the way you explained me Operator overloading in your last post right?), then operator overloading done or not? Why you are going round and round to the question then. Tell?

  • komputergeek

    @komputergeek-Yf5hJ4 Mar 1, 2009

    shalini_goel14it is good practice to avoid its usage in Java. .

    Most of the people suggest to avoid use of 'goto' even in languages other than java.What is the reason?

  • MaRo

    @maro-Ce3knx Mar 1, 2009

    komputergeekMost of the people suggest to avoid use of 'goto' even in languages other than java.What is the reason?

    #-Link-Snipped-#

  • gohm

    @gohm-F3UUpP Mar 1, 2009

    Even though the only thing I know of java is a warm, caffeinated beverage in a mug, this is a great thread!

  • shalini_goel14

    @shalini-goel14-ASmC2J Mar 1, 2009

    I request all the people here to refer following link.

    #-Link-Snipped-#

    It may clear the doubts of those who are not cleared about features removed in Java πŸ˜€

  • Raviteja.g

    @ravitejag-02nJVr Mar 1, 2009

    shalini_goel14Raviteja,
    I showed you an example right? There is no need of any operator overloading concept here right?

    In my shown example , you see that same "+" operator is adding two integers also as well as appending two Strings also(the way you explained me Operator overloading in your last post right?), then operator overloading done or not? Why you are going round and round to the question then. Tell?

    sorry mam,
    i did the same in my external lab exam but they said it is not appreciable
    so i asked you to say how to do operator overloading in matrices
    sorry if my way of asking is wrong!πŸ˜”

  • safwan

    @safwan-NH7W5Y Mar 3, 2009

    hi all ceans and specially mam ,
    but i am late offcorse i am not able to run my firs programm "Example.java" any sujjetion mam.

  • shalini_goel14

    @shalini-goel14-ASmC2J Mar 3, 2009

    safwanhi all ceans and specially mam ,
    but i am late offcorse i am not able to run my firs programm "Example.java" any sujjetion mam.

    No early late funda here safwan.πŸ˜€ Tell what problems you are getting and also tell what all steps you followed?
    Have you installed jdk on your system?

  • safwan

    @safwan-NH7W5Y Mar 4, 2009

    shalini_goel14No early late funda here safwan.πŸ˜€ Tell what problems you are getting and also tell what all steps you followed?
    Have you installed jdk on your system?

    i first downloaded jdk1.6 as your given link then i installed it on my directory D. where i had already made new folder with name myjava.
    so the directory will be D:myfolder
    then i started writing programm example.java but unluckily how to comile i dont know i saved it as example.java now what to do?πŸ˜”

    i think its not like vb where we have a code area and we can start debugging by pressing start button.so clear my doubt.

  • shalini_goel14

    @shalini-goel14-ASmC2J Mar 4, 2009

    Yes safwan, it is not like vb where running programs is darn easy.

    Could you see those black colour images shown by me in Day-1 class. ok leave them right now

    First let me confirm whether jdk is installed on your system or not ok
    Follow following steps right now

    Step 1: Go to Start Menu
    Step2: Click on Run
    Step 3: Enter "cmd"
    Step 4: Command prompt window opens and enter there "java -version"

    Now show me that image here-what it shows to you ok πŸ˜€

  • safwan

    @safwan-NH7W5Y Mar 5, 2009

    hi cmd showd me the following message when i typed java-version in it.

    'javaoversion'is not recognized as an internal or external command,operable program ot batch file.

    (sorry unable to insert image becose i dont know as it is asking url)

  • Yamini L

    @yamini-uMEVDQ Mar 5, 2009

    safwanhi cmd showd me the following message when i typed java-version in it.

    'javaoversion'is not recognized as an internal or external command,operable program ot batch file.

    Hey safwan,

    Did you type java-version or java -version??