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?
 
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)/
0
 
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).
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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).
0
 
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.
0
 
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.
0
 
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.
0
 
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
0
 
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.
0
 
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
0
 
Mayank SAssociate Director - Product EngineeringCommented:
BTW, I hope these were genuine concept-related doubts you had and not some home-work questions :)
0
 
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.
0
 
objectsCommented:
> 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

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

0
 
jedistarAuthor Commented:
or

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

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

All Courses

From novice to tech pro — start learning today.