Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8726579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:16:35+00:00 2026-06-13T08:16:35+00:00

In C++: class Rectangle { int x, y; public: void set_values (int,int); int area

  • 0

In C++:

class Rectangle {
    int x, y;
  public:
    void set_values (int,int);
    int area () {return (x*y);}
};

int main () {
  Rectangle rect;
  rect.set_values (3,4);
}

In Java:

class Rectangle {
    int x, y;
    void set_values (int x,int y);
    int area () {return (x*y);}
}

public static void main(String[] args) {

Rectangle rect=new Rectangle(3,4);

}

In C++ compiler will create rect object & reserve memory 4 bytes. I want to know How Java is creating object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:16:36+00:00Added an answer on June 13, 2026 at 8:16 am

    When you do: –

    Rectangle rect;
    

    in C++, it invokes, the 0-arg default constructor, provided by the compiler.

    If you wanted to use a 2-arg constructor, you would have to do: –

    Rectangle rect(3, 4);
    

    wherein, you would have to provide a 2-arg constructor explicitly, else it won’t compile.


    Now, similar is the case in Java. If you do: –

    new Rectangle();
    

    then its ok, because, in that case, compiler will provide you with a default 0-arg constructor, as in C++, but when your create object like: –

    new Rectangle(3, 4);
    

    Then you would have to explicitly provide the class with a 2-arg constructor, else it won’t compile, as in your 2nd example, that won’t compile.

    Also, once you provide a parameterized constructor, then in that case, Compiler won’t provide the default constructor. You would have to provide a 0-arg constructor explicitly, if you want to use one. This suffice in both, Java and C++

    Now, as per the memory allocation is concerned, since Java is platform independent, so the size of int type is 32 bits, in all platforms. This is in contrast to C++ where size of data types are platform dependent.

    See JVM Specification - The Structure of JVM for detaled information about the allocation of various types.

    P.S.: –

    I suggest you to go through the below link, for basics of Java classes and objects: –

    • http://docs.oracle.com/javase/tutorial/java/javaOO/index.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class CRectangle { int x, y; public: void set_values (int,int); int area (void); }
I have this code: public class Area { Texture2D point; Rectangle rect; SpriteBatch _sB;
Consider the following code: class Rectangle { public: // Constructors Rectangle(){ init(0,0); } Rectangle(int
Here's my code: class Ramka extends JFrame { public static final int SZEROKOSC =
I'm using the pimpl-idiom with std::unique_ptr : class window { window(const rectangle& rect); private:
Class 1 private void checkDuplicateCustomer(BulkCustomerVO bulkCustomerVO) { PagedDuplicateCustomerVO duplicateCustomerVO = new PagedDuplicateCustomerVO(); duplicateCustomerVO.setCustomer(bulkCustomerVO.getCustomerVO()); duplicateCustomerVO
I have the following class: GLRectangle.h #include XPView.h class GLRectangle { public: int top,
Here is the code I have written for Rectangle class. class Rectangle (l: Double,
In other words, given a base class shape and a derived class rectangle :
How to implement Shape class above Circle ? I mean Circle and Rectangle class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.