Lazy Thursday Question: Weird Syntax You've Seen?
Hey all,
We all know that Java is the best language ever and it always makes absolute syntactic and semantic sense, but occasionally I'll come across some syntax that makes me say, "huh?".
These often turn out to be awesome learning exercises, showing functionality I didn't know about before.
So I'd love to hear about syntax that threw you off or made you say "wtf, is this even Java...". I'll start with a few of mine.
The first one is pretty basic, operating on an array returned from a method directly instead of using a variable to reference the array first:
Code java:
public class ArrayTest {
public static int[] getArray(){
return new int[]{0, 1, 2, 3, 4, 5};
}
public static void main(String[] args) {
int[] array = getArray();
System.out.println(array[2]);
System.out.println(getArray()[2]);
}
}
Specifically, I had to think about this line for a second before realizing what was going on:
System.out.println(getArray()[2]);
Re: Lazy Thursday Question: Weird Syntax You've Seen?
I stumbled across another example when a coworker of mine asked why his static block wasn't working (he was missing the static keyword):
Code java:
public class BlockTest {
public static int x = 0;
static{
x = 2;
}
{
x = 4;
}
public BlockTest(){
x++;
}
public static void main(String... args){
new BlockTest();
System.out.println("X: " + x);
}
}
Specifically, when does the non-static block at the top of the class happen? What if the class is never instantiated and only contains static methods? The answer is actually in the basic tutorials, and it's called an initializer block. It gets copied into every constructor. Weird, but useful when you want to initialize stuff in every constructor but want to use code that can't be at the top with the declarations.
Re: Lazy Thursday Question: Weird Syntax You've Seen?
I remember Json posting some funny allowable names for classes/fields/methods.
http://www.javaprogrammingforums.com....html#post4807
Brief code snippets:
Code Java:
public class _ {
public int _ = 0;
public final int __;
public _() {
this._++;
this.__ = 5;
}
public static void main(final String... arguments) {
final _ _ = new _();
System.out.println("_ = " + _._);
}
}
Code Java:
public class $ {
public final int Ģ;
public int $ = 0;
public $() {
this.$++;
this.Ģ = 5;
}
public static void main(String... arguments) {
final $ $ = new $();
System.out.println("$ = " + $.$);
}
}
Re: Lazy Thursday Question: Weird Syntax You've Seen?
And finally, how do you create an instance of a public non-static inner class? This is how:
Code java:
public class InnerClassTest {
public InnerClassTest(){
OuterClass oc = new OuterClass();
OuterClass.InnerClass ic = oc.new InnerClass();
}
public class OuterClass {
public class InnerClass{
//inner class
}
}
public static void main(String... args){
new InnerClassTest();
}
}
The answer is you use the reference to the outer class, then call .new InnerClass().
What. That's so gross looking to me. In my brain, it would make more sense if it looked like this:
But I'm sure there's a reason for the syntax looking the way it does.
Anyway, those are the three weird syntax examples I've seen lately that made me stop and think for a second. Does anybody else have any others? I'd love to see them!
Re: Lazy Thursday Question: Weird Syntax You've Seen?
Quote:
Originally Posted by
helloworld922
I remember Json posting some funny allowable names for classes/fields/methods.
Hahahaha. Those are awesome. I'm going to start using them as often as possible at work and just wait for a code review...
Re: Lazy Thursday Question: Weird Syntax You've Seen?
Not necessarily weird syntax, but an abuse of Java's arrays to get pass-by-reference(ish) for primitives:
Code Java:
public class PrimitivesAbuse
{
public static int factorial(int n, boolean[] success)
{
if(n < 0)
{
success[0] = false;
return 0;
}
int answer = 1;
for(int i = 1; i <= n; ++i)
{
answer *= i;
}
success[0] = true;
return answer;
}
public static void main(String... args)
{
boolean[] success = new boolean[1];
int f = factorial(3, success);
if(success[0])
{
System.out.println("was able to compute 3!");
}
else
{
System.out.println("can't compute 3!");
}
}
}
Re: Lazy Thursday Question: Weird Syntax You've Seen?
And who can forget the abuse of =, +=, ++/--, ... etc?
Code Java:
public static void main(String[] args)
{
int a = 1, b = 2, c = 3;
c += b = a++;
System.out.println(a + " " + b + " " + c);
}
Re: Lazy Thursday Question: Weird Syntax You've Seen?
This is pretty interesting
Code Java:
public class Foo {
static int fubar = 420;
Foo getFoo() {
return null;
}
public static void main(String args[]) {
Foo foo = new Foo();
System.out.println(foo.getFoo().fubar);
}
}
Re: Lazy Thursday Question: Weird Syntax You've Seen?
Nice examples guys. I just thought of another one, referring to an outer class's "this" reference from an inner class:
Code java:
public class InnerClassTest {
public InnerClassTest(){
OuterClass oc = new OuterClass();
OuterClass.InnerClass ic = oc.new InnerClass();
}
public class OuterClass {
public int getInt(){
return 1;
}
public class InnerClass{
//inner class
public InnerClass(){
System.out.println(getInt());
System.out.println(OuterClass.this.getInt());
}
public int getInt(){
return 2;
}
}
}
public static void main(String... args){
new InnerClassTest();
}
}
It took me a minute to figure out how to get to the outer class's "this".
Code java:
System.out.println(OuterClass.this.getInt());
Re: Lazy Thursday Question: Weird Syntax You've Seen?
EDIT: Lol, I duplicated the one posted right above this post. It actually took me a ages to figure that one out, if I remember correctly. In any case, inner classes seems to be the source of many weird syntax examples :cool:.
Re: Lazy Thursday Question: Weird Syntax You've Seen?
I encountered this the other day. Appearently even anonymous inner classes can have some form of constructor like functionality. It just looks really weird, lol. For example:
Code java:
public class Program
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
List<String> strs = new ArrayList<String>(){{add("b"); add("a"); add("z");}}; // <-- Who gets this just by looking at it? I sure didnīt when I saw it, lol.
for (String s : strs)
System.out.println( s );
}
}
Re: Lazy Thursday Question: Weird Syntax You've Seen?
Quote:
Originally Posted by
Kerr
I encountered this the other day. Appearently even anonymous inner classes can have some form of constructor like functionality. It just looks really weird, lol.
Nice. Yeah, that's using an anonymous inner class along with the "initializer block" I posted in one of my examples. With formatting, that translates into this:
Code java:
import java.util.ArrayList;
import java.util.List;
public class Main
{
public static void main(String[] args)
{
List<String> strs = new ArrayList<String>(){
//initializer block
{
add("b");
add("a");
add("z");
}
};
for (String s : strs){
System.out.println( s );
}
}
}
What makes it more interesting is that you can't have a normal constructor in an anonymous inner class!
Re: Lazy Thursday Question: Weird Syntax You've Seen?
Not necessarily weird syntax, but abusing the finally clause can produce some really weird results:
Code :
try {
return true;
} finally {
return false;
}
The above code actually returns false...
This one however returns true:
Code :
for(int i=0;i<10;i++) {
try {
return false;
} finally {
continue;
}
}
return true;
Re: Lazy Thursday Question: Weird Syntax You've Seen?
Found this on the internet.
Code java:
public class JTest
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
System.out.println( "Hello" );
http: //www.javaprogrammingforums.com/forum.php
System.out.println( "World" );
}
}
This actually compiles. Can you see why? It is pretty easy to understand, just a case of when we humans see one thing and the compiler sees another. Had to read the explanation myself before I got it xD.
Also, this looks kind of weird, but it is valid code for returning an array.
Code java:
public class JTest
{
private String getSomeStrings()[]
{
return new String[]{"a", "b", "c", "d"};
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
JTest var = new JTest();
String[] strs = var.getSomeStrings();
for (String tmp : strs)
System.out.println( tmp );
}
}
Re: Lazy Thursday Question: Weird Syntax You've Seen?
Lol, yeah, it would actually have been better with normal compilers in my opinion. This just seems so weird when you first look at it.