This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a few q's

 
charlie mills
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic