jump to navigation

Testing WordPress Post via Email May 5, 2015

Posted by sodce in General.
add a comment

Something here.

JAVA INTERVIEW QUESTIONS February 20, 2012

Posted by sodce in Java.
Tags: ,
add a comment

A great source for Java Interview Questions.

JAVA INTERVIEW QUESTIONS.

Java – Final February 4, 2012

Posted by sodce in Java.
add a comment

Just as static keyword mean different things for fields, methods, and classes, final keyword also mean different things for fields, method, and classes.

A final field means that the value of the field can not be changed after it is initialized. A final field can be initialized at the time it is declared or at the time of a constructor. Say you have a bank account number. After you initialize the account number you really don’t want anyone or any method to change the value of the account number. It is permanent once it is set.

A constant such as a the value of PI would be final but also static. A static final field is considered a Constant and must be initialized at the time it is declared. A static final or constant field should be all capitalized to bring home the point it is a constant. So here is an example….

public static final double PI = 3.14;

A final method simply means that it can not be over ridden in a sub-class. So if I have a bankAccount class that has the getAccountNumber method and it is declared as final. Inside of a savingAccount class that extends the bankAccount class, I can not create another method called getAccountNumber.

A final class simply states you can not create a sub-class of that class. So in other words, you can not use this class in an extends statement. The String and Math class provided by Java are examples of a Final class. Try extending the Math class and you are sure to get a compile error.

Java – this() and super() February 4, 2012

Posted by sodce in Java.
add a comment

What is this() in Java? What is super() in Java? How are they related and yet different. They both work with constructors.
this() is the ability to call a different constructor within the same class.
super() is the ability to call a constructor of the super-class or parent class from within a sub-class.
Usage of “this()”:

public Class Ball {
 public Ball() {
     this(45); 
 }

 public Ball(int size) {
     
 }
}

Usage of “super()”:

public Class BasketBall extends Ball {
   public BasketBall(int ballsize) {
      super(ballsize);  
   }
}

Java – Static February 3, 2012

Posted by sodce in Java.
Tags: ,
add a comment

Static is a keyword used before a class, field or method. They mean different things.

A static field means that it is put in the static memory and not the dynamic memory area. It also means you can share it among classes. So if I create a field and declare it as “public static int count = 0; for a dog class”, I’m actually creating a single field regardless of how many dog object I create. This field count is shared among all of the dog object. So in other words you can say the field is a field of the class and not a field of the object. This type of thing is very useful when you want to keep track of how many object you have created or when you want to increment the value to provide a unique value for each object. For instance a bank account number.

A static method similar to a static field is associated with a class and not an object. This would mean that I could call the method without having to instantiate the object. So instead of doing Dog thedog = new Dog(); thedog.bark(); , I could do Dog.bark(); and that is it. If the method works with the class fields then the class fields must be static as well.

A static constructor is used when we need to initialized an array or need to use some logic to initialize the field. Since an array needs to have a loop in order to initialize the values, we would use the following construct.

static {

}

A static class simply states that it can not be instantiated and it must contain only static fields and static methods. The Math class is a good example of such a static class.

Java Constructors December 15, 2011

Posted by sodce in Java.
Tags:
add a comment

Java constructors instantiate an object. It creates an object out of the class you created. If you don’t provide a constructor then Java automatically creates a zero argument constructor. If you create a constructor then Java does not create a zero argument constructor. Constructors has the same name as the class and does not have a return type. So don’t put a “void” in as a return type. The compiler will fail to compile the statement.

There can be more than 1 Java constructor for a single class.  This is called method overloading.  When overloading a constructor, they are to have different parameters than the other constructors.  This is what allows the compiler know which constructor to run.  Constructors are usually public in scope.

public class BankAccount {
   private int acctNumber;
   private double balance;

   public BankAccount(int accountNumber, double beginBalance) {
       acctNumber = accountNumber;
       balance = beginBalance;
   }
}

Java Class and Object December 9, 2011

Posted by sodce in Java.
Tags: ,
add a comment

Java is an object-oriented programming language and not a procedural or scripting language.  C, Pascal, Cobol, Fortran can be viewed as a procedural language and Perl, Python, Bash can be viewed as a scripting language.  Procedural language has a top down design in which it steps through each line of code and execute them in waterfall sort of way.  Scripting language is similar with a waterfall design.  Procedural language are compiled programs and compiled performance has to best performance.  A scripting language is not compiled but execute each line of code as it is stepping through it.

C++ and Java is object-oriented.  So what is object oriented and how is it different from procedural language?  Object Orientation is designed with the object in mind first and the actions that can be performed on it.  Encapsulation of both the data type and the operations that can operate on those data type.  Procedural language has the data types and functions but they are not tightly coupled as they are in object orientation.  However, I have worked with the C programming language and know you can make your program in a way that it is close to being object-oriented.  A C structure is like a user defined type.  Inside of a structure you can specify a pointer to a function.  This would be similar to a class in Java.  I won’t go into details as to all of the features of object-orientation.  This could be a topic for another day.  Today focus is how to create a Java Class.

A java class is like a blueprint and a java object is like the built product.  So you need to define the blueprint first and then in your program, create the object or in other words, instantiate the object.  To create a java class you simply start it with the word “class”.

public class Ball {
    //instance variables;

    //methods to act on the variables;
}

The “public” is called the scoping of the data type. A data type, a class is considered a user defined data type, can have a public scope which means it is visible and re-usable by other programmers. There are also private scope but in most cases when you create a class it will be “public”.
Instance variables are variables declared in a class and usually has the form of <scope> <data type> <variable name> = <value>. The scope and value are not required. If no value is provided then the value will be defaulted to a value provided by the JVM. It is good practice to provide a default value or insure a constructor mutates this variable.

public class Ball {
    private int radius = 0;
}

Again the variable radius has a scope of “private”. This is generally what all instance variables of a class should be scoped as. This is saying that a programmer who use the object should not have direct access to the variable and change it without going through the class’s methods (another word for operations/functions).

Method of a class is like a function in any other library yet has a scope as well.  So the general syntax of a method call is() {}.  The method operates on the data of the class and in the previous example will operate on the variable radius.  Generally there are two types of methods.  The  accessors and the mutators.  The accessors are those method that returns the state of the variables.  The mutators are method that manipulate the state of the variables.

public class Ball {
    private int radius;

    public boolean drawBall() {
        // code to draw the Ball using the radius variable;
    }
}

We now have a blueprint of the class Ball. As an object oriented programmer, I don’t really care about the variable of the class Ball. However I do care about what the class method can do for me and what I will get back. What the method does with the variable radius has no concern for me. Now let create the actual product or object.

public class DrawTheBall {
     public static void main(String[] args) {
        Ball theBall = new Ball();
        theBall.drawBall();
     }
}

Let’s go over this previous statement. You can say that everything starts with a class. This class has the main method and which indicate this is the program to execute. Pay attention to the main method syntax. This is the syntax to use every time.

Notice now that I created a variable called “theBall” with the data type of “Ball”.  I instantiated an object of data type Ball using the keyword “new” and assigning it to the variable theBall.  The variable theBall is now referencing an object on the stack of data type Ball.

There is something wrong with these examples.  Can you identify it?  Basically there is no constructor and the default value of radius is the number zero.  So calling drawBall() probably will throw an error.   Later I’ll discuss constructors and the garbage collection.

Java Conversion and Casting December 1, 2011

Posted by sodce in Java.
Tags:
add a comment

Java basic data type such as int, byte, double, float, long can be automatically converted in the assignment or arithmetic equation. Take for example if you have a long variable as an lvalue and you assign an interger to the lvalue variable it will automatically be converted to a long before the assignment.

long longValue;
int intValue = 9;
longValue = intValue;

When assigning a larger variable type value to a smaller variable type value, the java compiler will complain and request you to do a cast. To do a cast you basically put the type in a curve bracket before the value.

For instance:

int intValue;
long longValue = 2147483649
intValue = (int)longValue;

However this is not always a good thing to do unless you have a very good reason to use a cast. Java can truncate or wrap. We all know what truncate means in which the right side of the values get chopped off. This is usually the case with decimal or fractional numbers. Wrap on the other hand is when the number specified that is assigned to a smaller variable size would wrap and start at the beginning. The example I gave above is 2 greater than the maximum of an int and the results would be “-127”. So again cast when you know what you know you need to.
One more thing on conversion. When adding/multiplying/dividing/modulating two variables, the smaller variable is converted up to the larger variable size.
For instance:

long A = 217;
int B = 5;
long C = A + B;

Since A is a long and a long is larger than an int, B is first converted up to a long from an int and then added to A and assigned to C.

Consider what happens when you do the following cast. I’ll leave it up to you to figure out the results so fire up your editor and compile and run the code.

double theDecimal = 23.5D;
int theResult = (int)theDecimal;

Java Questions November 30, 2011

Posted by sodce in Java.
Tags:
add a comment

Here I’m going to list a bunch of questions, these questions should provide you as an aid as you are mastering the Java Programming Language. This post will be updated from time to time so please come back to it or “Follow” the post.

1. How is Java “Pointer” different from C pointers?
2. Are the variable types size of Java such as an int dependent on the type of machines (Intel, Sparc, Mac)?
3. What does the term “Compile Once, Run Anywhere” mean?
4. What is the size of a char variable type?
5. How do you initialize a char variable type?
6. How do you initialize a char variable type with a tab space?
7. What is a variable called when it is declared within a function?
8. What happens when you use an uninitialized variable in your java program?
9. What are the two most common form of commenting your code?

Java Data Types September 21, 2011

Posted by sodce in Java.
Tags: , , ,
add a comment

The Java Programming Language has two categories of  data types.  The first one being the basic data types and the other one being the class types.   They behave differently mainly when we get to the point of talking about passing by value and passing by reference in a function call.  In Java there is no such thing as pointers so you won’t see any special character preceding the variable name when they are a parameter of a function.

The basic types are:  int, short, long, byte, boolean, char, float, double. The basic types are “Passed-By-Value” when they are listed as a parameter of a function.

The object types are the “User Defined Types”  that the user creates with class preceding the name of the type.  For instance:

Class Stock {
data.....
}

The Java Language provides over thousands of class types that programmers can re-use.  Class types are “Passed-By-Reference”.

Let talk a little bit about the “Pass-By-Value” and the “Pass-By-Reference”.  Take for instance these two segments of the code.


int totalcnt;
totalcnt = 9;
incrementCnt(totalcnt);
print totalcnt;


public void incrementCnt(int theCnt) {
theCnt++;
}

You would think that the output would be 10.  It will print “9” the reason is that the totalCnt passes the value and not the reference to totalCnt variable.  Since totalcnt is declared as an int which is a Java basic type, then it is passing the value “9” to the function incrementCnt.  Once it enters the function it doesn’t care a thing about the totalcnt variable.

However if we changed the totalcnt variable to be of type “Integer” which is a Java class type that is provided with the Java programming language then we will get a very different result.


Integer totalcnt;
totalcnt = 9;
incrementCnt(totalcnt);
print totalcnt;


public void incrementCnt(Integer theCnt) {
theCnt++;
}

The output here will now be “10”. This is called “Pass-By-Reference”. As I stated earlier, Java doesn’t have pointers. So incrementCnt copies the reference “address” of totalcnt or in other words theCnt points to the same object as totalcnt. A change to theCnt value is also changing the totalcnt value since they both point the same object.

So instead of thinking of pointers as you would do in the C/C++ programming language, think of the data types in Java. If it is a basic data type then it will be “Pass-By-Value”, otherwise it is a “Pass-By-Reference”.