File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes a few q's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a few q Watch "a few q New topic
Author

a few q's

charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
I have a couple of random questions:

1.does it matter if i write Integer or int?

2.I'm not sure what this phrase means: declaration and initialisation of singleton String objects

3.iterating through a fixed-size collection using a loop - a loop using for would be included in this?

4.using a method to output to the screen - is this just println?
5.use of arithmetic operators - i assume this is just using + - etc.
6.use of String operators - is this using + to add strings together?
7.use of String methods - i'm not sure what this means?

thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

1. does it matter if i write Integer or int?


Of course it matters! One is an object type. The other is a primative type. Just because autoboxing can somewhat hide some of the details / differences, doesn't mean that you should rely on it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Daclaration of a field is like this:

private int number;

and initialisation is like this (assuming this is the first time you have assigned "number"):

number = 123;

And you can put the two together into a single line:

private int number = 123;

Yes, you can use a for-loop for a Collection, with an index or an Iterator, or a while loop with an Iterator.

Arithmetic operators mean +-*/ and % (usually).
The only operators you can use on String that I can remember are + and ==; the latter test whether you have two references to the same object, so equals() is usually better.

For methods in String you will have to go through the API. There are lots of them!
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

Originally posted by charlie mills:
[QB]

1.does it matter if i write Integer or int?

Yes. It's like Henry said. By the way, when using Lists, you can't create an list of int, just Integers (not that does matter. Search for Wrappers in Java and you'll find out that int i = Integer(2); it's possible).


Originally posted by charlie mills:
2.I'm not sure what this phrase means: declaration and initialisation of singleton String objects

Me neither. A Singleton is a Design Patter from GoF's book. That pattern says that you can instantiate only one instance of that class. If you search at Google's, you'll find a good code explaining that (I guess that Singleton is the simplest pattern).


Originally posted by charlie mills:
3.iterating through a fixed-size collection using a loop - a loop using for would be included in this?

Yep!

A good way to iterate is using the "for-each".

Originally posted by charlie mills:
4.using a method to output to the screen - is this just println?

Not necessarily. When you create a Frame you're outputing something in the screen too, aren't you? Using println you're doing that too.

Originally posted by charlie mills:
5.use of arithmetic operators - i assume this is just using + - etc.

Yep. "+", "-", "/", "%".

Originally posted by charlie mills:
6.use of String operators - is this using + to add strings together?

I guess it is. This question depends on the context of the text (I'm really sure that what I wrote is wrong, sorry). What kind of operators? There's the "+" to concatenate Strings objects and the methods that come with the API.

Originally posted by charlie mills:
7.use of String methods - i'm not sure what this means?

Using methods from the String class. Search Google for the API of String class.


[edit]
Sorry Campbell. I was writing when you post.
[/edit]
[ November 02, 2008: Message edited by: Andre Brito ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
You don't actually need the Singleton pattern for the java.lang.String class.

If you say private String myName = "Campbell";, every time you use "Campbell" in the same application the JVM will treat a bit like a Singleton and use the same object. And sorry I overlooked the question about screen output; Andre Brito has give a good answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a few q's