We help IT Professionals succeed at work.

variables of non-primitive types, constructors IO

jedistar
jedistar asked
on
Medium Priority
437 Views
Last Modified: 2010-03-31
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!
Comment
Watch Question

Principal Technologist
CERTIFIED EXPERT
Commented:
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)/

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
BTW, I hope these were genuine concept-related doubts you had and not some home-work questions :)

Author

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 Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> 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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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.
Top Expert 2004

Commented:
>> 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

Author

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?

Author

Commented:
or

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

output:
test100
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
yes that is corerect
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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 :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.