Orientation to Objects in Java

Asked

Viewed 138 times

0

I’m new to Java and I’d like to understand how it works. Like, how I use classe inside the other (and what’s it called)?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

For example: it is sending the Bundle in the method but he is not a String neither a int or similar. How exactly this works?

And another. When we create a variable String, for example:

String a = "aa";

Why don’t we need to use the operator new and what happens if we stop using it on some object?

  • 1

    Your second question is answered here: http://answall.com/q/19098/3117 . Always try to keep only one question per topic, if the link I gave you answer your question you can remove it by editing your question.

  • Your first question is confused. Do you understand how methods work? Well, the parameter in this case is an object of the Bundle type, rather than being a int or a String. What you want to know how it works in this?

  • Yes.. That’s what’s leaving me in doubt.. Why not an int or string but a Bundle? Like it’s not a primitive value. How it works?

  • So what you want to understand then is how do objects work? Have you ever created a class? Initialized it? Called it methods? Objects are like primitive types, only more complex, rs.. They’re like a struct enhancement of C. Ah, and String in Java is a class, it’s not primitive like int or the double.

  • So... But I don’t understand how I can use this in a method... How am I going to manipulate this object being a parameter of a method? Do you understand my doubt? P

  • 1

    I really don’t understand no, rs.. You use it within a method just as if you instantiated it within the method, only in that case it was instantiated somewhere else. Like primitives, you can create a new int within a method, or you can take one per parameter.

Show 1 more comment

1 answer

2

Buddy! Here the space is a little short to explain everything to you! I suggest some booklet ( http://www.caelum.com.br/apostila-java-orientacao-objetos/ ).

This method you showed, it belongs to an Android project, right? So, who creates an instance is the application. That when initializing the screen . xml, sends this Bundle to the method!

On the second question: When it’s done String a = "aa"; is the same as String a = new String("aa");, because when the compiler processes/reads the "" understands itself as new String("");

  • I agree with @Thiago Luiz Domacoski. Look for a good workbook, course, there are several, if English is not a problem for you, from a look at Oursera, there are excellent courses there. Harvard has also offered free online courses. https://www.coursera.org/

  • obg but type did not find where it talks about it.. What name does one give when we pass an object as a method parameter? (to find it easier)

  • I don’t know if I understand, but see method overload!

  • Type.. onCreate(Bundle saveInstance) I am passing as parameter an object not a primitive value as int or char understands? ex onCreate(int saveInstance) Like I want to know what the name of this is so I can dig deeper into this

  • 1

    His first question wasn’t properly answered, you just said who created it, but you didn’t explain how it works. The answer to the second question is wrong, initializing a string with new and without new are not the same things.

  • Oi, Thiago, this edition you suggested It wasn’t approved because it conflicted with one of mine. I’m ringing because your suggestion left out a giant block of code that was hidden... Worth checking out this Faq from time to time How we should format questions and answers? ;)

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.