Link to home
Start Free TrialLog in
Avatar of jedistar
jedistarFlag for Singapore

asked on

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!
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
However, in Java 5, you can add a primitive type directly because it uses auto-boxing/ unboxing (automatically converts it to a wrapper class).
>> 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).
>> 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.
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.
>> 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.
>> 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
>> 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.
>> 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
BTW, I hope these were genuine concept-related doubts you had and not some home-work questions :)
Avatar of jedistar

ASKER

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

>> 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.
Avatar of Webstorm
Webstorm

>> 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
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?

or

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

output:
test100
yes that is corerect
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 :)