Author Topic: Some Java questions  (Read 7673 times)

0 Members and 1 Guest are viewing this topic.

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Some Java questions
« on: May 04, 2013, 05:13:18 pm »
What is the difference between an interface and an abstract class?
The only difference I can see is that all methods in an interface are abstract, while abstract class can contain both concrete and abstract methods.

Can you define fields inside an interface or abstract class, and what would those be used for?

What the heck does Encapsulation mean?

What is the difference between a static method and a non-static method?

Is this an example of an enhanced for loop?
Code: [Select]
String[] maga= {"you", "lost", "the", "game"};
for(String omni:maga}
     System.out.println(omni);

« Last Edit: May 04, 2013, 05:15:06 pm by epic7 »

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: Some Java questions
« Reply #1 on: May 04, 2013, 10:08:35 pm »
What is the difference between an interface and an abstract class?
The only difference I can see is that all methods in an interface are abstract, while abstract class can contain both concrete and abstract methods.
Other than that and the syntax, the only real difference is that a class can implement any number of interfaces but can only extend one abstract/non-abstract class.

Can you define fields inside an interface or abstract class, and what would those be used for?
In Java you can only define static fields in an interface.  Any class that implements that interface then gets those fields "for free."  In an abstract class, you can define any field for the exact same reasons you define fields in normal classes.

What the heck does Encapsulation mean?
In OOP, this means that you don't/can't access the internal details (private, or otherwise restricted, fields and methods) of a class.  This is useful because whoever writes a class doesn't have to worry about someone else changing some fields incorrectly and putting it in an invalid state.  It's useful for whoever uses a class because they don't need to worry about changing some fields incorrectly and putting it in an invalid state.  Also, this allows someone to rewrite the class and implement it internally in a completely different way, without breaking compatibility with existing code.

What is the difference between a static method and a non-static method?
Non-static methods have access to a variable called "this" which means they can access all other methods and fields of that class.  Static methods do not have this variable, so they can only access static methods and fields of that class (unless they obtain an instance of that class).  The difference when calling them is:

ClassName.staticMethod();
ClassName objectReference = ...;
objectReference.nonStaticMethod();

Is this an example of an enhanced for loop?
Code: [Select]
String[] maga= {"you", "lost", "the", "game"};
for(String omni:maga}
     System.out.println(omni);
Yes, also known as a for each loop.

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Some Java questions
« Reply #2 on: May 04, 2013, 10:30:19 pm »
Thanks- that pretty much clears it up :D

And fields in interfaces are implicitly static and final, right?

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: Some Java questions
« Reply #3 on: May 04, 2013, 10:40:45 pm »
It appears so.

Offline dinosteven

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 194
  • Rating: +10/-1
    • View Profile
Re: Some Java questions
« Reply #4 on: May 05, 2013, 11:48:56 pm »
I've got a Java question too, but was too lazy to make a new topic. :P
What's the deal with Strings? Are they Objects or are they primitive data structures? I was under the impression that they were Objects, but you can do some stuff with them that you never could with any other objects.

Offline jacobly

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 205
  • Rating: +161/-1
    • View Profile
Re: Some Java questions
« Reply #5 on: May 05, 2013, 11:59:03 pm »
All primitive types start with a lowercase letter, and as long as you follow Java naming conventions, all object types start with a capital letter.  You can actually do special things with many of the classes in the package java.lang.  For example, you can say Integer i = 5; and i would be an object.

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Some Java questions
« Reply #6 on: May 05, 2013, 11:59:22 pm »
Technically, it's not a primitive type, but I still kinda like to think of it as one... Idk

(ninja'd)
« Last Edit: May 06, 2013, 12:00:29 am by epic7 »

Offline Goplat

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 289
  • Rating: +82/-0
    • View Profile
Re: Some Java questions
« Reply #7 on: May 06, 2013, 10:14:30 pm »
Technically, it's not a primitive type, but I still kinda like to think of it as one... Idk

(ninja'd)
The big difference between a primitive type like int and an object type like String (or Integer) is that the former simply contains data while the latter contains a reference to an object stored somewhere else in memory. In particular, the == operator used on object types will return whether the two references point to the same object rather than whether they point to identical objects. (This is rather noxious for Integer: Java keeps a cache of low-valued Integer objects so == can appear to work for a while, then stop working once the code starts dealing with numbers greater than 127.)
Numquam te deseram; numquam te deficiam; numquam circa curram et te desolabo
Numquam te plorare faciam; numquam valedicam; numquam mendacium dicam et te vulnerabo

Offline dinosteven

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 194
  • Rating: +10/-1
    • View Profile
Re: Some Java questions
« Reply #8 on: May 06, 2013, 10:29:35 pm »
Okay... I think I get it.
I also have some more questions (our teacher went over topics and we all pretended we knew everything to avoid embarrassment :P).
What's a javadoc and everything to do with it?
What's Autoboxing (and will it be on the AP test)?
I took a practice and got 69.3 - a 5 is 60-80. Do you think I'm safe from getting a 4 if I make dumb mistakes?

Offline blue_bear_94

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 801
  • Rating: +25/-35
  • Touhou Enthusiast / Former Troll / 68k Programmer
    • View Profile
Re: Some Java questions
« Reply #9 on: May 06, 2013, 10:33:57 pm »
I think Javadocs are documentation for methods. Not sure, though, so ask someone else.
Due to dissatisfaction, I will be inactive on Omnimaga until further notice. (?? THP hasn't been much success and there's also the CE. I might possibly be here for a while.)
If you want to implore me to come back, or otherwise contact me, I can be found on GitHub (bluebear94), Twitter (@melranosF_), Reddit (/u/Fluffy8x), or e-mail (if you know my address). As a last resort, send me a PM on Cemetech (bluebear94) or join Touhou Prono (don't be fooled by the name). I've also enabled notifications for PMs on Omnimaga, but I don't advise using that since I might be banned.
Elvyna (Sunrise) 4 5%
TI-84+SE User (2.30 2.55 MP 2.43)
TI-89 Titanium User (3.10)
Casio Prizm User? (1.02)
Bag  東方ぷろの

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Some Java questions
« Reply #10 on: May 07, 2013, 02:20:48 am »
Javadoc
Javadoc is the documentation of stuff. You can provide a number of tags with it to give information about what it does. A sample could look like:
Code: [Select]

/**
*  @author ElementCoder
*  This class is an example.
*/
public class Example
{
  /**
  *  @author ElementCoder
  *  @version 1.0
  *  @param par1 first parameter
  *  @param par2 second parameter
  *  Method description here.
  */
  public void myMethod(int par1, String par2){}
}
These are not all the tags you can use, there are more. If you're using Eclipse, the javadoc of a method (if there is any) will show up when hovering over the method. Otherwise it's usually exported along the api/lib in browse-able html format.

Autoboxing
Autoboxing is when primitives are automatically converted to their respective Object form. Its counterpart is unboxing, converting an Object to its primitive. Small example:
Code: [Select]
ArrayList<Integer> myList = new ArrayList<Integer>();
myList.add(1); //Autoboxing
myList.add(2); //Autoboxing
int x = myList.get(0); //Unboxing
When storing in the list the ints 1 and 2 get converted to Integers automatically. The 4th line automatically converts the Integer to int. All primitives have an Object counterpart:
int - Integer; boolean - Boolean; byte - Byte; char - Character; float - Float; double - Double; long - Long; short - Short
Edit: added autoboxing.
« Last Edit: May 07, 2013, 02:38:22 am by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Some Java questions
« Reply #11 on: May 07, 2013, 02:12:49 pm »
Okay... I think I get it.
I also have some more questions (our teacher went over topics and we all pretended we knew everything to avoid embarrassment :P).
What's a javadoc and everything to do with it?
What's Autoboxing (and will it be on the AP test)?
I took a practice and got 69.3 - a 5 is 60-80. Do you think I'm safe from getting a 4 if I make dumb mistakes?
What? 60-80 out of 100? O.O O.O

Offline dinosteven

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 194
  • Rating: +10/-1
    • View Profile
Re: Some Java questions
« Reply #12 on: May 07, 2013, 02:23:03 pm »
What? 60-80 out of 100? O.O O.O
It's raw score. The score is calculated like
round(A+1.11(B+C+D+E),0)
Where A is the number of questions right out of 40,
B,C,D,and E are the Free Response scores out of 9 each.
So the highest possible score would be round(40+39.96,0) or 80 points.

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Some Java questions
« Reply #13 on: May 07, 2013, 03:48:59 pm »
Oh :P
But still, if 60/80 was the minimum for a 5, you'd only need to get 75% correct