2 Replies - 6924 Views - Last Post: 20 October 2013 - 01:39 PM

#1 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Demystifying confusing Java terminology.

Posted 20 October 2013 - 05:20 AM

I'm interested in listing and explaining terminology and other things, in simple terms, that confuse new Java programmers. It would be great to get others involved as well. Please submit explanations of things that confused you.

CHECKED EXCEPTION - A checked exception is any exception that is checked by the Java compiler at compile time. Your programs will not compile until these exceptions are handled. e.g. having to surround FileWriter in a try/catch block.

FileWriter w = null;
		try {
			w = new FileWriter("file.txt");
		} catch (IOException e) {
			e.printStackTrace();
		}



UNCHECKED EXCEPTION - Basically, any exception that is not checked by the compiler. These exceptions are the ones that blow up in your face at run time. Hint: RuntimeException - NullPointedException, IndexOutOfBoundsException, etc.

TYPE ERASURE - Fancy name for a simple compiler trick. All it means is that when you write with generics in Java:

List<String> groceries = new ArrayList<String>();
groceries.add("milk");
String s = groceries.get(0);



The compiler turns it to this:

List groceries = new ArrayList();
groceries.add("milk");
String s = (String) groceries.get(0);



The bytecode for both examples are identical due to "type erasure". Nothing complicated.

Is This A Good Question/Topic? 2
  • +

Replies To: Demystifying confusing Java terminology.

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Demystifying confusing Java terminology.

Posted 20 October 2013 - 05:25 AM

It will be good to get in early that Java != Javascript :)
Was This Post Helpful? 1
  • +
  • -

#3 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: Demystifying confusing Java terminology.

Posted 20 October 2013 - 01:39 PM

Term:

Reflection

Formal Definition:

In computer science, reflection is the ability of a computer program to examine (see type introspection) and modify the structure and behavior (specifically the values, meta-data, properties and functions) of an object at runtime. ~Wikipedia

Explain:

When I first began programming, I saw this term used continuously in development forums and tutorials, but despite attempts to demystify it, I could never fully understand it. I knew bits and pieces, but I couldn't use the term confidently.

Reflection can occur on different levels, but most commonly it's referred to mean programmatically examining (and possibly modifying) an object's definition at runtime. Determining an object's type, super type, interfaces, and methods, or adding a method to an object's prototype (in Javascript), are all examples of reflection.

Examples:

  • The instanceof operator in Java. You can do a lot more with the reflection API. This is examining code at runtime.
  • Self-modifying code in viruses. This is modifying existing code at runtime.
  • The eval() function in Javascript. This is adding code at runtime.


Advantages:

Reflection is used in many frameworks and libraries to achieve things that would otherwise be impossible to do. Some examples are:

  • Software Testing: Defining units tests and properties via annotations.
  • IOC frameworks (Spring, EJB): Locating registered objects via annotations.


Disadvantages:

  • Reflective operations are slower.
  • It's hard to infer about the behavior of reflective code, which excludes it from compile-time checks.
  • May allow access to private members, which can be a security issue.


Code Sample:

Spoiler

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1