• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a few q's

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 95
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
ice is for people that are not already cool. Chill with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic