• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

A bundle of JQPlus questions

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have several questions and hope that someone could help me.
Q.1
//in fileA.java
package p1;
public class A {
protected int i = 10;
public int getI(){return i;}
}
//in fileB.java
package p2;
import p1.*;
public class B extends p1.A
{
public void process(A a)
{
a.i = a.i*2;
}
public static void main(String [] args)
{
A a = new B();
B b = new B();
b.process(a);
System.out.println(a.getI());
}
}
Why the object b cannot access i ?
-----------------------------------------------------------------------------------------------------------------------------
Q.2
public class Test {
public static void main(String [] args)
{
int ia[][] = {{1,2}, null};
int ij[][] = (int[][])ia.clone();
System.out.println((ia==ij) + " ");
System.out.println(ia[0]==ij[0] && ia[1]==ij[1]);
}
}
Why is the result: true, false. When should I use the object.clone()?
-------------------------------------------------------------------------------------------------------------------------
Q.3
public class Test {
public Test(){
s1 = sM1("1");
}

static String s1 = sM1("a");

String s3 = sM1("2");
{
s1 = sM1("3");
}

static
{
s1 = sM1("b");
}

static String s2 = sM1("c");
String s4 = sM1("4");



public static void main(String [] args)
{
Test it = new Test();
}

private static String sM1(String s)
{
System.out.println(s);
return s;
}
}

Why the results are a b c 2 3 4 1? Why '1' will be the last one to be displayed?
--------------------------------------------------------------------------------------------------------------------------
Q.4
public class Test{
public static void main(String [] args)
{
int x = 4;
int a [][][] = new int[x][x=3][x];
System.out.println(a.length + " " + a[0].length + " " + a[0][0].length);
}
}
Why the answer is 4 3 3 ? Why the last 3rd dimension of array is 3?
-----------------------------------------------------------------------------------------------------------------------------
Q.5
public class Test{
public static void main(String [] args)
{
String str = "111";
boolean a[] = new boolean[1];
if (a[0]) str = "222";
System.out.println(str);
}
}
Why the a[0] represents false?
-----------------------------------------------------------------------------------------------------------------
Thanks for your kind help.
Andrew
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew,
In the future it mght be better to post each question in its own thread that way people in the future can find them easily by their subject and also each explaination can get long and it easier to keep them separate to make them easier to read.
Q1--
Check out this link for a previous discussion on this topic.
There have been many others too. Click on the search link at the top of this page and type in 'protected implementation' (without the quotes) as your search words, you'll get a few other links that might help you out.
Q2--
The output of the code you posted should actually be false, true. The == will test to see if the two arrays are the same (basically if they occupy the same location in memory) in this case they dont, the first comparison is false. The second comparison should be true though, because it is comparing primitives (by value).
Q3--
According to the JLS section 8.7 the static intializers are executed when the class is first loaded and the they are executed in the order they occur. That is why you get the a,b,c listed first. After that the steps in the JLS section 12.5 are followed.
-- instance initializers and instance variable initializers for this class are executed
-- the body fo the constructor is finished.
Q4--
When an array is created the dimension creation expression are checked from left to right and evaluated in the same order. Each expression is fully evaluated before the next one is. So the expression you posted has x = to 4 initially but the second dimension expresion assigns 3 to x so for all expression after that x is 3 not 4.
Q5--
When an array is created the JLS section 15.10.1 explains that when the array is created, right after the space is allocated the elements of the array are intialized to their default values, in this case the default for a boolean would be false.
[ April 15, 2002: Message edited by: Dave Vick ]
[ April 15, 2002: Message edited by: Dave Vick ]
 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your help. I understand Q.2-5. But, I cannot see the link you attached. Would you mind tell me briefly about the concept of protected implementation?
Thanks
Andrew
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew
Sorry about the link, I fixed it now. But here is a quote of Janes that explains it pretty well:

'protected' is a strange beast JLS �6.6.2 states:

quote:
--------------------------------------------------------------------------------
6.6.2 Details on protected Access
A protected member or constructor of an object may be accessed
from outside the package in which it is declared only by code
that is responsible for the implementation of that object.
--------------------------------------------------------------------------------
Which basically means that the subclass can only access the protected members of the superclass if it actually creates and uses the superclass member. You can't just pass a superclass object to one of it's methods.

 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for help.
Andrew
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, let me see if I have this right. The statement b.process(a) fails because the object reference by a is typed as an A object but really is a B object?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the statement b.process(a) fails because the declared typed of a in "a.i" is A, and not the same type in which this access expression ocurrs (B), or one of its subclasses. In this way, the b object cannot access a field for which it is not responsible, given that it was created in the super class A.
If the declaration of the parameter a would have been "B a", now the access check would have been allowed because the declared type of a is the same as the class in which ocurrs. Or, in the same speech as JLS: the b object would be able to access now a field (otherwise) declared in the class B, for which it is responsible; after all it was initialized in its constructor.
Just a minor thought:


The second comparison should be true though, because it is comparing primitives (by value).


ia[0] and ij[0] are pointing to the same object, that is {1, 2}. While ia[1] and ij[1] are holding the same reference: null.
The primitives are in ia[0][0] and ij[0][1]

[ May 18, 2002: Message edited by: Jose Botella ]
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic