?
Solved

Java questions

Posted on 2006-04-03
10
Medium Priority
?
274 Views
Last Modified: 2010-03-31
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?

thanks.
0
Comment
Question by:jedistar
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16360316
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16360321
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:zzynx
ID: 16360350
>> 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;
0
 
LVL 37

Expert Comment

by:zzynx
ID: 16360364
>> 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.
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 200 total points
ID: 16360391
The Final Word On the final Keyword: http://renaud.waldura.com/doc/java/final-keyword.shtml
Static, Final, and Enumerated Types in Java: http://www.informit.com/articles/article.asp?p=349047&rl=1
Controlling Access to Members of a Class: http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 200 total points
ID: 16360550
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:

http://java.sun.com/docs/books/tutorial/
0
 
LVL 3

Accepted Solution

by:
CI-Ia0s earned 600 total points
ID: 16378031
>>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;
ArrayList.add(x);

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>();
strings.add("Hello");
strings.get(0).substring(3);
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();
strings.add("Hello");
((String)strings.get(0)).substring(3);

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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16518938
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question