Java questions

Some basic questions on Java,

What is the public static void final each mean?

Why do we declare null for variables eg: int values = null; ?

What is autoboxing, generics etc?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

zzynxSoftware engineerCommented:
>> Why do we declare null for variables eg: int values = null; ?
int values = null;
is incorrect.
You can only set Object variables to null:

    String str = null;
zzynxSoftware engineerCommented:
>> Object variables
I mean, variables of a class that extends the Object class.
int, long, char are native types. You can't set these to null.
zzynxSoftware engineerCommented:
The Final Word On the final Keyword:
Static, Final, and Enumerated Types in Java:
Controlling Access to Members of a Class:
Mayank SAssociate Director - Product EngineeringCommented:
You can set it to 0, like: int i = 0 ; though its not required as that's the default value. Probably you need a basic Java tutorial:
>>What is the public static void final each mean?

public: anything can access this method, parallel classes, subclasses, parent classes
static: can be called from the class without creating an instance of the class. In other words, you can just say MyClass.method() instead of MyClass anInstance = new MyClass(); MyClass.method();
final: unchangable. You usually assign a value on declaring a final variable. After declaration, the variable can not be changed. If you don't assign a variable, the default is null (for Objects; 0 for native types (false for booleans)).

>>Why do we declare null for variables eg: int values = null; ?
I think he meant int[] values = null; The reason we declare things as null is that we sometimes write code that branches in a complex manner, and though that code (usually) guarantees that values[] will be assigned a value (an array of ints), the compiler doesn't know this. It gets worried that values[] won't have a value and will throw a NullPointerException when used later in the code. By assigning it the value of null, we make the compiler happy, even though it would still throw a NullPointerException if not later assigned a value.

>>What is autoboxing, generics etc?
Autoboxing is the new 1.5.0 feature that makes things easier for us when working with native types (ints, doubles, chars, etc) as Objects (Integer, Double, Character, etc). Previously (1.4.2 and earlier), the following code would produce an error as native types and Objects didn't mix. Now, it works great!
java.util.ArrayList list = new java.util.ArrayList();
int x = 5;

In the above example, the int x (which has the value of 5) is boxed into an Integer (we don't see this part) and added to an ArrayList. Before 1.5.0, we would have had to do list.add(new Integer(x)); It works the other way too. We can do:
Integer x = new Integer(5);
int y = 7+x;
x is automatically unboxed to be added to 7.

Generics allows us to work with a List or another data structure without type-casting. We tell the List that everything we add will be of a certain class. Then, when we get Objects from that List, those Objects are known to be of that class. For example:
ArrayList<String> strings = new ArrayList<String>();
We can call substring directly on the returned Object since we know it is a String.

Before generics, that code would have looked like this:
ArrayList strings = new ArrayList();

Every time we got an object from the ArrayList, it would need to be type-casted (i.e. the "((Stringstrings.get(0))" ). Generics removes this necessity which makes things easier when programming.

Anyway, this is just my personal explanation. Many of the linked sites offer far more accurate/in-depth explanations.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.