questões legais

essa é sobre passagem de params
bom divertimento,

class Value
{
public int i = 15;
}

public class TestPassArguments
{
public static void main(String argv[])
{
TestPassArguments t = new TestPassArguments();
t.first();
}

public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}

public void second(Value v, int i)
{
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
}

teoricamente se tem dois minutos para fazer cada questão, essa aqui é para poucos, eu levei quase 5 minutos, analizando ate responder… acertei a questão, mas com custo de tempo altíssimo

class MyParent
{
int x, y;

MyParent(int x, int y)
{
this.x = x;
this.y = y;
}

public int addMe(int x, int y)
{
return this.x + x + y + this.y;
}

public int addMe(MyParent myPar)
{
return addMe(myPar.x, myPar.y);
}
}

class MyChild extends MyParent
{
int z;

MyChild (int x, int y, int z)
{
super(x,y);
this.z = z;
}

public int addMe(int x, int y, int z)
{
return this.x + x + this.y + y + this.z + z;
}

public int addMe(MyChild myChi)
{
return addMe(myChi.x, myChi.y, myChi.z);
}

public int addMe(int x, int y)
{
return this.x + x + this.y + y;
}
}

public class MySomeOne
{
public static void main(String args[])
{
MyChild myChi = new MyChild(10, 20, 30);
MyParent myPar = new MyParent(10, 20);
int x = myChi.addMe(10, 20, 30);
int y = myChi.addMe(myChi);
int z = myPar.addMe(myPar);
System.out.println(x + y + z);
}
}

essas duas aqui tbm são interessantes, precisa testar para ver a saída.

esta

public class AQuestion
{
public void method(Object o)
{
System.out.println(“Object Verion”);
}
public void method(String s)
{
System.out.println(“String Version”);
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}

e esta

public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println(“StringBuffer Verion”);
}
public void method(String s)
{
System.out.println(“String Version”);
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}

Imprime “String Version”

por que?

Ricardo

a tua pergunta acima tbm é uma das minhas, achei essa questão num mock exam, e não consegui entender como é a regra

aqui tem outra questão chata,

interface One
{
public void someMethod();
}
public class One_impl implements One
{
public native void someMethod();
public static void main(String args[]) {

  }

}

porque ela compila e roda sem erros ? e da runtime error quando o metodo someMethod é chamado ?

ordem do initializer, baita pega ratão…

class TestOrderInitializer
{

int i = getInt();
int k = 20;
public int getInt() { return k+1; }
public static void main(String[] args)
{
TestOrderInitializer t = new TestOrderInitializer();
System.out.println(t.i+" "+t.k);
}
}

essa questão aqui pra mim imprimiu nulltrue, truenull e nullnull, alguem sabe a mora da história ?

class TestNull
{
public static void main(String[] args)
{
System.out.println(null + true); //1
System.out.println(true + null); //2
System.out.println(null + null); //3

}

}

mais uma que é preciso ter muita antenção…

public class TestClass
{
public static void main(String[] args) { calculate(2); }
public static void calculate(int x)
{
String val;
switch(x)
{
case 2:
default:
val = “def”;
}
System.out.println(val);
}
}

outra quebra cuca

class InitTest
{
public static void main(String[] args)
{
int a = 10;
int b = 20;
a += (a = 4);
b = b + (b = 5);
System.out.println(a+ " "+b);
}
}

[quote=“balrog”]essa questão aqui pra mim imprimiu nulltrue, truenull e nullnull, alguem sabe a mora da história ?
[/quote]

pelo que sei qualquer tipo primitivo pode ser concatenado com o operador + e o null tb…

lembre-se que o operador + sempre cria uma nova String… que é o resultado que foi impresso

me corrijam se eu falei besteira…

Ricardo

ATENÇÃO :!:

Isso já foi discutido neste fórum no tópico “string concatenation”.

O método toString() só é chamado automaticamente com dois operandos do operador binário + quando um dos operandos for uma referência para um objeto da classe String. No caso, true + null ou qualquer primitivo + null deveria dar um erro de compilação.

Mais detalhes:

http://developer.java.sun.com/developer/bugParade/bugs/4717193.html

Tentem atualizar o sdk ou então usem o jikes :wink: que serve fiel à JLS

essa questão tbm é interessante, testem e vejam o que acontece…

boolean b1 = false;
boolean b2 = false;
if (b2 != b1 = !b2)
{
System.out.println(“true”);
}
else
{
System.out.println(“false”);
}

Pra mim deu erro de compilação:

C:marciolxjava2003cert>javac TestePrec.java
TestePrec.java:5: unexpected type
required: variable
found   : value
                if (b2 != b1 = !b2)
                       ^
1 error

C:marciolxjava2003cert>java -version
java version "1.4.0_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0_01-b03)
Java HotSpot(TM) Client VM (build 1.4.0_01-b03, mixed mode)

isso mesmo !!
acontece porque ele executa primeiro b2 != b1, que resulta false e depois ele tenta atribuir !b2 para false, que não é nenhuma variável.