• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

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!
0
jedistar
Asked:
jedistar
  • 4
  • 4
  • 2
  • +1
3 Solutions
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now