[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

variables of non-primitive types, constructors IO

Posted on 2006-04-06
19
Medium Priority
?
415 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!
0
Comment
Question by:jedistar
  • 13
  • 3
  • 2
  • +1
19 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 2000 total points
ID: 16392357
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16392389
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Expert Comment

by:Mayank S
ID: 16392398
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16392409
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16392423
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16392436
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16392451
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16392473
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16392499
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16392506
BTW, I hope these were genuine concept-related doubts you had and not some home-work questions :)
0
 

Author Comment

by:jedistar
ID: 16397169
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
 
LVL 92

Expert Comment

by:objects
ID: 16397316
> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16397999
>> 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
 
LVL 13

Expert Comment

by:Webstorm
ID: 16398933
>> 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
 

Author Comment

by:jedistar
ID: 16412016
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
 

Author Comment

by:jedistar
ID: 16412029
or

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

output:
test100
0
 
LVL 92

Expert Comment

by:objects
ID: 16413203
yes that is corerect
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16415759
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

873 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