What are types in the return of a method?

Asked

Viewed 127 times

-9

I used PHP and I’ve never seen it, we know there are types of return double, float, int, boolean.

In Java when we want to return one int I’ll tell you what:

public int soma(int numero1,int numero2) {
return numero1+numero2;

}

Now what I’m not getting is that I recently saw something new in these return types. I’m seeing codes that returns type variable Connection of the kind. What does that mean?:

public static Connection obterConexao() {
     Connection conn = null;
   try{
    conn = DriverManager.getConnection("jdbc:mysql://:3306/banco","root","senha");
   }catch(SQLException e) {
       System.out.println("erro ao conectar");
   }
   return conn;
}

Will return string, int, double? How does it work?

Also this static I didn’t see in PHP.

Not only that, I’ve seen several codes using returns other than int, float, double, boolean, they are using variable name as so this? How to use it? What’s the use?

  • Please edit the question to limit it to a specific problem with sufficient detail to identify an appropriate answer.

  • At the points you mentioned, there is no difference between PHP and Java. In both languages it is possible to create new types by the programmer, and these types can, of course, constitute the return value of a method or function. In your case, the function obterConexao returns an object of type Connection (or an instance of the Connection class). As for static, PHP also allows the creation of static methods, i.e., that belong to the class and not to a specific instance.

  • Thank you Vander

  • 2

    @Edenilson could explain his Edit? It seems to me that the posting was vandalized, and this is not allowed here. I am waiting a few moments before performing the procedure in these cases, in case it was just a mistake.

  • can delete , after a few responses I received,

  • 1

    The procedure is not exclusion, but reversal and warning not to do again. Once posted, the content is part of the community’s collective effort. More details on [help] and the terms you accepted when creating the account. About the answers, the vote ranks as the community deems useful (more details on [tour]).

  • blz ta all right

Show 2 more comments

1 answer

5

The question is confusing and shows that you are learning without a structure. This does not work, you learn everything by half, wrong things, skips steps. It’s like building a house without a foundation, missing bricks. I don’t advise doing that. You have learned some items, now you will learn another that has been missing, but others will continue to be missing. Even the text lacks structure, more structure is needed in code.

That’s exactly a guy, nothing more than that. If you learned what kind is a list of half a dozen names, you learned wrong. A type is what defines how a data should be or behave. The language has some own, the language library has others, third party libraries also have, and your application will create several others.

In PHP it is also like this, although a little more limited. In PHP 7 and 8 it is not so limited (despite being a gambiarra of the language), so it is not something of Java, it is programming in general (plus).

We have no way of knowing what the guy is Connection because they can have several types with that name in different contexts. The standard library has an interface with that name, which is still a type. Looking at the documentation, you should always do, can see more about it.

But you may not understand what it is interface, how does the Connection. But that’s because you skipped too many steps. You have to start with simpler things. Can’t write an entire programming book here to talk about everything that’s missing to know.

If you take bad examples you will learn wrong. This looks like one that does wrong (very common), but I don’t have enough information to say. Today on the internet is a bad place to learn, because one does wrong, becomes popular, and everyone copies the wrong making it look right. This has destroyed the ability of people to learn right. Whoever is starting is not able to separate good content from bad content. One issues an opinion, you like it and you think it’s true.

The static also has in PHP, the fact that you have not seen it is just that you have not learned PHP well enough. And you start to have too many questions in the same post. I’ve answered a lot about this, other people as well (but not all organize to be easy to find). Search the site that almost everything you want has been asked before. It’s not the best way to learn yet, but it helps. You should still look for a quality, modern book.

You can search about the most popular questions. Can go only in Java, but will lose many important concepts as an example: In programming, what is an object?, What is typing style? and What is the difference between a static and dynamic programming language?.

Finally: How a machine identifies the data type?. But there are others about typing. Just reinforcement to learn first the concepts, the fundamentals, the most basic mechanisms, only to then go to more sophisticated things. You don’t learn second-degree equation when you don’t know arithmetic. The problem is learning methodology.

Browser other questions tagged

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