variables of non-primitive types, constructors IO

what are vars of non-primitive type for?

i.e Integer ten = new Integer(10)

Whats the purpose of the Integer(10)?
Do we have to declare them null initially always?

2. Why do we declare package xxx; on top..

3. What happens if there is no constructors for a class?
How do we declare multiple constructors to point to
other constructors? Can constructors have params?

4. In I/O, what is Integer.parseInt(), StringTokenizer for?
also what is the flush in writer.flush() ;

5. Whats the sequence of evaluation for
int val = 10 * 5 / 5 + 2 % 1

thanks!
jedistarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Mayank SAssociate Director - Product EngineeringCommented:
It is a wrapper class. For example, you cannot write an 'int' directly to a collection like an ArrayList or a Hashtable because they expect objects (not primitive data-types like ints/ floats) so Java has provided wrapper classes (which extend java.lang.Object) and inside an object of this class you can wrap a primitive value (like an int/ float), then use it like an object (like add it to a collection)/

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
Mayank SAssociate Director - Product EngineeringCommented:
However, in Java 5, you can add a primitive type directly because it uses auto-boxing/ unboxing (automatically converts it to a wrapper class).
Mayank SAssociate Director - Product EngineeringCommented:
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Mayank SAssociate Director - Product EngineeringCommented:
>> Do we have to declare them null initially always?

Not always. If you don't know what value you wanna assign immediately (but wanna do it later), you can assign them null (or let them remain unassigned - the default is null).
Mayank SAssociate Director - Product EngineeringCommented:
>> Why do we declare package xxx; on top..

Classes are divided into packages in Java because people might write classes with the same name (e.g., DateUtilities) and then it will be a problem for the JVM to decide which class it has to use. So dividing the class into packages helps modularize it.
Mayank SAssociate Director - Product EngineeringCommented:
Generally the name of a package consists of company-name, project-name, module-name, etc, like com.yourcompanyname.projectname.modulename.ClassName - generally it would be unique if this convention is followed.
Mayank SAssociate Director - Product EngineeringCommented:
>> What happens if there is no constructors for a class?

Java provides a default one.

>> How do we declare multiple constructors to point to other constructors?

What do you mean by pointing? You can call another constructor using this () or you can call a super-class (base-class) constructor using super ()

>> Can constructors have params?

Yes.
Mayank SAssociate Director - Product EngineeringCommented:
>> In I/O, what is Integer.parseInt(),

Converts a String to an integer. e.g., if you have a String "1234", then parseInt () will return an 'int' with the value 1234. Read the documentation: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html
Mayank SAssociate Director - Product EngineeringCommented:
>> StringTokenizer for?

StringTokenizer is used for parsing tokens if you wanna split a String based on some delimiter, e.g., if you have String a = "12,34,45" and you wanna extract 12, 34, 45 out of it, you can use a StringTokenizer and split them. Java 1.4 onwards, you can do this directly from the String class using the split () method.
Mayank SAssociate Director - Product EngineeringCommented:
>> also what is the flush in writer.flush() ;

Buffered writers often maintain the data in buffers (memory) and write it to the actual destination (like, say, the disk) when you call flush ().

>> Whats the sequence of evaluation for
>> int val = 10 * 5 / 5 + 2 % 1

Figure out yourself :) the operator precedence is defined here:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
Mayank SAssociate Director - Product EngineeringCommented:
BTW, I hope these were genuine concept-related doubts you had and not some home-work questions :)
jedistarAuthor Commented:
yeah if u realise the way i ask them, its no no home work ;)

for the package, so if we wanna make Class A part of package xxx;
we put "package xxx;" on top of Class A in A.java?

What is the default constructor?

What if Integer.parseInt() parses a String which is "abcd" and not "1234"

How do we use StringTokenizer and split()?

for flush, therefore when its flushed, the data is no longer is buffer and written to dest?

thanks.
Mick BarryJava DeveloperCommented:
> What is the default constructor?

A constructor without any args

> What if Integer.parseInt() parses a String which is "abcd" and not "1234"

It throws an exception'

> How do we use StringTokenizer and split()?

http://javaalmanac.com/egs/java.util/ParseString.html
http://javaalmanac.com/egs/java.util.regex/ParseLine.html

Mayank SAssociate Director - Product EngineeringCommented:
>> therefore when its flushed, the data is no longer is buffer and written to dest?

Yes.

>> A constructor without any args

Actually there is a small difference. We often use the word default constructor for the no-argument constructor that we write. If you provide a 0-argument constructor in your class, it is not called as the default constructor. The default constructor is the one which Java provides in case you don't have any constructors in your class.

>> so if we wanna make Class A part of package xxx; we put "package xxx;" on top of Class A in A.java?

Yes.
WebstormCommented:
>> so if we wanna make Class A part of package xxx; we put "package xxx;" on top of Class A in A.java?
Yes, and directory path where your class is stored must be the same as package (replacing . by / or \ (Windows)), for example :

package com.sun.tools;        //   ->  <path>/com/sun/tools/A.java
public class A
{
...
}

->  <path> must be added to your classpath when using com.sun.tools package
jedistarAuthor Commented:
neat las thing:

system.out.println("100" + 42);
system.out.println(100 + 42);
system.out.println(100 + "42");

output:
10042
142
10042

is this correct?

jedistarAuthor Commented:
or

int x = 100;
System.out.println("test" + x);

output:
test100
Mick BarryJava DeveloperCommented:
yes that is corerect
Mayank SAssociate Director - Product EngineeringCommented:
Because + is used for concatenating Strings too (if one of the operands is a String, the other is converted to a String).

BTW, if you have more questions, you can open a new one :)
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
Java

From novice to tech pro — start learning today.