Arrays

1. Whats the difference between these 2?
String[] days = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};
String[] days = new String[] {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"};
As in, when do we use the 2nd line, when we can declare the first line already.

2. float or double for 3.14
why is it double and not float for value declaration 3.14?

3. Whats the diff between local variables and normal variables?

4. What is the purpose of declaring static int = 5; than just int = 5;

5. For the code below, is there something wrong with the tempclass constructor?

class TempClass {
   int i;

   public void TempClass(int j) {
      i = j;
   }
}

public class C {
   public static void main(String[] args) {
      TempClass temp = new TempClass(2);
   }
}

thanks!
jedistarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hoomanvCommented:
> when do we use the 2nd line

consider you have a method
public static void f(String[] arr) {}
you cant do
f({"a", "b"});
you have to do
f(new String[] {"a", "b"});
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hoomanvCommented:
> What is the purpose of declaring static int = 5; than just int = 5;

static variables are class variavles. you can access these vars by Class name, no need to instantiate and access the var from instance. there is only one instance of static variables so each instance of the class that changes that variable, would affect all instances of that class, all of them will see the change
0
Manikandan ThiagarajanSenior consultantCommented:




 >>public void TempClass(int j) {
  >>    i = j;
  >> }
this should be

TempClass(int j)

{
i=j;
}

return type of constructor is object

Local variables

Local variables accessed with in a loop

normal variable is accessed by anywhere in the program


you declare float like

float f = 3.14f;
>>static int = 5; than just int = 5;

in static varialle one instance of memory created, that would be overrided.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

hoomanvCommented:
> For the code below, is there something wrong with the tempclass constructor?
YES
public void TempClass(int j) {
should be
public TempClass(int j) {
constructor does not return anything
0
Manikandan ThiagarajanSenior consultantCommented:

>>1. Whats the difference between these 2?
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21724594.html

this link could be helpful to you
0
jedistarAuthor Commented:
2. Do you have examples of local variables and normal variables.
why do we have to differentiate them, can't we jus use normal vars?

3. therefore if i declare float vars, they all need to be xx.xxxf with f behind?
wherelese double can be xx.xx or xx.xxxx i.e 3.148549 anytime?

4. oh ok what does the void do to my TempClass?
0
sciuriwareCommented:
Local variables only exist in and during a method. Instance variables exist as long as their Object,
static variables live as long as your program.

Java is smart enough to understand  3.14 by itself.


;JOOP!
0
sciuriwareCommented:
That was 3 & 4 together.
0
sciuriwareCommented:
5. A constructor is never void: it must return a new object!

;JOOP!
0
hoomanvCommented:
> 5. A constructor is never void: it must return a new object!
but without return statemente you mean

> therefore if i declare float vars, they all need to be xx.xxxf with f behind?
> wherelese double can be xx.xx or xx.xxxx i.e 3.148549 anytime?
no. assigning to float var would do this for you. float f = 3.14 is correct
constants which we use without defining any variable for them will be automaticaly considered as double unless you prefix them by 'f'.
0
sciuriwareCommented:
{:>)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

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.