0% found this document useful (0 votes)
9 views5 pages

Java Coding Assignment: Bitwise & This Keyword

Uploaded by

Parkhi Acchreja
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Java Coding Assignment: Bitwise & This Keyword

Uploaded by

Parkhi Acchreja
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Assignment 2
Computational Coding with Java Standard

Student Name: Parkhi Acchreja UID: 22BCS14694


Branch: CSE Date of Performance: 01/06/24

1) Write a code to demonstrate how bitwise operator works.


public class bitwise {
public static void main(String[] args) {
int num1 = 60; // 60 = 0011 1100
int num2 = 13; // 13 = 0000 1101
int result;

result = num1 & num2;


[Link]("num1 & num2 = " + result );

result = num1 | num2;


[Link]("num1 | num2 = " + result );

result = num1 ^ num2;


[Link]("num1 ^ num2 = " + result );

result = ~num1;
[Link]("~num1 = " + result );

result = num1 << 2; // shift left by 2 bits


[Link]("num1 << 2 = " + result );

result = num1 >> 2; // shift right by 2 bits


[Link]("num1 >> 2 = " + result );

result = num1 >>> 2; // shift right by 2 bits, preserving sign


[Link]("num1 >>> 2 = " + result );
}
}

Code:-

Output:-
2) Write a code to demonstarte the uses of this keyword
public class this_keyword_usecases {
int x; // instance variable

// Constructor
this_keyword_usecases(int x) {
this.x = x; // using 'this' to refer to the instance variable
}

// Method
void display() {
[Link]("Value of x: " + this.x); // using 'this' to refer to the instance
variable
}

// Inner class
class InnerClass {
void innerMethod() {
[Link]("Value of x from inner class: " +this_keyword_usecases.this.x);
// using 'this' to refer to the outer class instance
}
}

public static void main(String[] args) {


this_keyword_usecases obj = new this_keyword_usecases(16);
[Link]();

this_keyword_usecases.InnerClass innerObj = [Link] InnerClass();


[Link]();
[Link]("Parkhi Acchreja!");
}
}
Code:-

Output:-

3) How does static and local inner class work? Demonstrate.


4) Demonstrate the difference of reusing a method and overriding a method in
inheritance. Also discuss the uses of super keyword

DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Common questions

Powered by AI

The 'this' keyword in Java is instrumental in instance management as it refers to the current object, making it clear which object's instance variables or methods are being accessed or modified. In contrast, 'super' is used within a subclass to call methods or access variables from its direct superclass, hence facilitating code reuse and method overriding. By enabling subclasses to extend and tailor superclass behaviors, 'super' supports polymorphism while promoting cleaner, more manageable code structures in inheritance hierarchies. These keywords articulate inheritance, as 'this' supports encapsulation within the current object, while 'super' bridges the specialization-spectrum with the parent class's generality .

Method overriding in Java permits a subclass to provide a specific implementation for a method already defined in its superclass, enabling polymorphism. This allows objects to invoke the overridden method appropriate to their runtime type rather than the compile-time type. The 'super' keyword is used in subclasses to access methods from the superclass that have been overridden. This is beneficial for cases where a subclass needs to extend or slightly modify the behavior of a superclass method while still leveraging its functionality. 'super' can be crucial in avoiding code duplication and maintaining a clean and manageable inheritance hierarchy .

Static inner classes do not require an instance of the outer class; they can be instantiated independently, which provides flexibility in code use, allowing the class to be associated with the containing class rather than a particular instance. This is useful for logical structures that signify a one-to-one relationship with their outer class but without the dependency on the outer class's instance. In contrast, non-static or local inner classes can access the outer class's instance members directly, making them useful for logically grouping classes that operate on the instance data of the outer class. Choosing one over the other depends on whether the inner class needs access to instance-level data or if it's purely a logical grouping mechanism .

Inner classes in Java are pivotal for encapsulating functionality and tightly coupling its use with specific instances of the outer class, making the design more cohesive. By logically grouping class definitions within classes, Java supports encapsulation and unity, crucial attributes of object-oriented programming. Inner classes have direct access to instance variables of their outer class, simplifying communication and reducing dependencies on getters or setters. This fosters a high degree of cohesion and encapsulation, beneficial for scenarios requiring confined logic that’s an intrinsic part of a top-level class. Inner classes also enrich the object-oriented design by allowing nested structures that synergize with polymorphism, inheritance, and encapsulation principles .

The decision between using an instance inner class versus a static nested class in Java hinges on the relationship and dependencies the inner class has with the enclosing class. Instance inner classes, which hold a reference to an instance of the outer class, are ideal when the inner class needs to access and modify the outer class's instance variables. Static nested classes don’t have this reference, making them suitable for cases where the inner class should not be tied to an outer class instance, effectively encapsulating behavior that relates to the class as a whole rather than an instance. The choice affects encapsulation, design clarity, and performance, as static classes avoid the overhead of maintaining a reference to their containing class's instances .

Bitwise operators enhance efficiency and speed in Java by performing operations at the binary level without the overhead of traditional arithmetic operations. They can simplify complex operations into simple shifts and bitwise logic, offering performance improvements, especially for tasks involving calculations that can leverage power-of-two optimizations, such as bitmap handling, flag checking, or low-level protocol implementations. Despite their efficiency, they often lead to more complex and less-readable code, hence their use is usually reserved for performance-critical sections .

The 'this' keyword in Java is crucial for differentiating between instance variables and parameters or other local variables when their names conflict. In the provided code, 'this.x = x;' assigns the parameter 'x' to the instance variable 'x', clarifying the context where 'x' is being altered. It is also valuable in referencing outer class variables within inner classes, improving code readability and maintainability. By using 'this', developers convey which variables are associated with an object's specific state, enhancing understanding for future maintenance and debugging .

In object-oriented programming, especially in Java, constructors initialize new instances of a class. Using the 'this' keyword in constructors clarifies that the constructor parameters are being assigned to instance variables, reducing errors and improving clarity, especially when parameter names coincide with instance variable names. It enhances the self-documenting aspect of the code by explicitly showing which data fields are part of the object's state. 'this' also supports constructor chaining, where one constructor calls another, promoting code reuse and simplifying initial setup tasks .

Combining bitwise with shift operators in Java allows direct manipulation of data at the binary level. For instance, in the provided code, using 'num1 & num2' performs a bitwise AND, yielding 12 (0000 1100 in binary) because only the bits where both numbers have 1s are set to 1. Performing a left shift ('num1 << 2') effectively multiplies 'num1' by four, resulting in 240 (1111 0000), which aligns with the binary move, adding zeros on the right. Conversely, a right shift ('num1 >> 2') divides 'num1' by four, resulting in 15 (0000 1111), discarding lower bits and propelling higher-order bits. Combining these operators is potent for operations such as fast multiplications, divisions, and toggling specific bits without additional arithmetic overhead .

Bitwise operators in Java provide a way to perform operations on binary representations of integers. They are used for low-level programming tasks that require direct manipulation of binary data. For instance, the bitwise AND (&) operator compares each bit of two binary numbers and returns a new number with bits set to 1 only where both input bits were 1. Similarly, the OR (|) operator sets a bit to 1 if it is 1 in either operand, and the XOR (^) returns 1 where bits are different. The NOT (~) inverts each bit, whereas shift operators like << and >> are used to shift bits left or right, modifying the number by powers of two .

You might also like