This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Please explain this format.  RSS feed

 
Dimi Baer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. Can someone explain to me why these are formatted the way they are formatted?

Scanner myScanner = new Scanner(System.in);

java.util.Scanner myScanner = new java.util.Scanner(System.in);

InputStreamReader isr = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(isr);



I think it'll help me to grasp Java better if I understand this format of [blah] [blah's name] = new [blah something]. What is "new" doing there? What is [blah]? What am I doing with [blah something]? I haven't been able to find an explanation for this format. It's always glossed over.

Your assistance will be much appreciated.
 
Christopher Donovan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forgive my limited knowledge - but i will try to help

Scanner (this is a class) myScanner(This is an object) = new(this is callin the constructor in the Scanner class) Scanner(System.in) (this is the constructor in the Scanner class)

what you are doing is creating an object (myScanner) of type (Class)Scanner that will read in data (System.in) as its argument.

i hope this helps - until someone can give a better explanation
 
Dimi Baer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so it's

[Class][Object] = [Call to constructor][Constructor]


Great ... Now I just have to figure out what a constructor does. I should be able to work my way to that. Thanks!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


  • 1st Scanner : Variable type.
  • myScanner : Variable name. (Not object name. Objects don't have names.)
  • new : new operator, means we're creating a new instance of a class.
  • 2nd Scanner : Name of the class we're instantiating.
  • (...) : Types and order of parameters indicate which constructor signature we're invoking. A constructor is similar to a method, but is not a method. It is invoked at object creation time to put the new object into a valid initial state.
  • System.in : The public "in" member variable of the System class that is the "standard InputStream" for the program, typically connected to the terminal from which the app is launched. This is a parameter that is passed to the constructor.
  • = : takes the value of the expression on the RHS and assigns it to the variable on the LHS.


  • Put it all together and we're declaring a variable named myScanner that is of type Scanner and initializing it with a reference to a newly created Scanner object, to which we pass the standard input for that Scanner to use.

    And of course, all of this is spelled out in any decent text or tutorial.
     
    Dimi Baer
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okay, so it's

    [variable type][variable name] = [new instance][class instanced]

    Thanks.

    Jeff Verdegan wrote:And of course, all of this is spelled out in any decent text or tutorial.


    I know it's a really stupid question. I need things spelled out for me a few several times until they click. I'm slow that way.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dimi Baer wrote:
    [variable type][variable name] = [new instance][class instanced]


    It's the new keyword, not "new instance". The new instance is the object that's created. "Instance" and "object" are pretty much synonymous.


    I know it's a really stupid question. I need things spelled out for me a few several times until they click. I'm slow that way.


    It's fine to ask the questions, and no problem if you need need some additional explanation for things to sink in. That's why we're here, after all. We just ask that you do some research on your own before posting.
     
    Dimi Baer
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeff Verdegan wrote:
    Dimi Baer wrote:
    [variable type][variable name] = [new instance] [new keyword][class instanced]


    It's the new keyword, not "new instance". The new instance is the object that's created. "Instance" and "object" are pretty much synonymous.


    Okay, I seem to not understand what an object is, or how any of these things relate to it.

    We just ask that you do some research on your own before posting.


    I've read through Java for Dummies (I know ...) and I've availed myself of other online resources ... and I've read the introductory chapters of a few other books I have ... but I just can't seem to understand the basics. Something's not sticking. I'm probably just being impatient. I'll read through the books again and see if things become clearer. Thanks for trying to help, anyway. I'll revisit this thread again in a few days if I'm still confused.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Dimi Baer wrote:
    Jeff Verdegan wrote:
    Dimi Baer wrote:
    [variable type][variable name] = [new instance] [new keyword][class instanced]


    It's the new keyword, not "new instance". The new instance is the object that's created. "Instance" and "object" are pretty much synonymous.


    Okay, I seem to not understand what an object is, or how any of these things relate to it.


    A class is like a template, or a blueprint, or a cookie cutter. It defines the details of what goes into a particular kind of object, and what it can do.

    An object is a thing that's made to follow the specifications defined by the class. It's the cookie you cut out using the cookie cutter.

    So, for example, a Person class might define a name field and a birthDate field, and maybe a calculateAge() method. Every time we do new Person() we create a Person object that has the fields defined by the Person class (a Person has a name and a birthDate), and can do what the Person class says it can (a Person can calculate its own age). So we might call new Person() twice, creating 2 Person objects, and setting one's birthDate to sometime in 1990 and the other one to sometime in 2000. If we call calculateAge() on the first Person, we'll get a rsult of about 22 years, and when we call it on the second Person object, we'll get a result of about 12 years.

    An object holds state (name and birthdate--each Person has his own name and birthDate) and behavior (each person can calculate his own age).
     
    Dimi Baer
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ... I think I finally understand.


    Scanner myScanner = new Scanner(System.in)
    ~
    [variable type][variable's name] = [instantiate][object of variable type]
    ~
    This is the variable type, and this is its name; I would like for you to instantiate it as an object that I can use.


    And


    object : class : programme
    as
    resources (employees, equipment, etc.) : department : company


    The resources (objects) of a department (class) are working toward a particular aim for the company (programme), and the company coordinates the various departments via messages and data.

    For example
  • R&D receives directives from the executives
  • R&D performs its duty, and reports the results to the executives
  • the executives tell the production and marketing departments what to do with the information from the R&D department
  • the production and marketing departments do their thing
  • the company's goal is achieved


  • Yes?

    Thanks, Jeff, your last post, combined with a bunch of other videos and books, have sorted me out. Much appreciated.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!