0% found this document useful (0 votes)
3 views6 pages

Typecasting and Anagram in Java

The document explains typecasting in programming, detailing implicit and explicit casting with code examples. It also includes a program to check if two strings are anagrams and demonstrates how to create a deadlock in Java. Additionally, it provides a solution for storing distances to cities in a map and printing them out.
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)
3 views6 pages

Typecasting and Anagram in Java

The document explains typecasting in programming, detailing implicit and explicit casting with code examples. It also includes a program to check if two strings are anagrams and demonstrates how to create a deadlock in Java. Additionally, it provides a solution for storing distances to cities in a map and printing them out.
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

1. What is Typecasting explain with small code snippet?

 Typecasting is converting data from one data type to another data type.

 Typecasting can be applied to both primitive and non-primitive types.

 Typecasting process doesn't alter the data but alters the type.

There are two types:

 Implicit casting

 Explicit casting

Implicit casting: This is known as automatic typecasting. Usually, this happens when you
converting a lower data type to a higher data type

Example Code Snippet

class Main {

public static void main(String args[]) {

int i = 10;

float f = i;

[Link]("Value of f = " +f);

Output:

Value of f = 10.0

Explicit TypeCasting

Explicit casting involves assigning a data type of high range to a lower range. This casting
needs to be done manually

Example Code Snippet

class Main {

public static void main(String args[]) {

float f = 3.142f;

int i = (int) f;

[Link]("Value of i = " +i);

}
}

Output:

Value of i = 3

Note: Why do we need typecasting?

Typecasting is needed to get access to fields and methods declared on the target type or
class. You cannot access them with any other type

Write a program to check if two strings are an anagram

An anagram is if two strings are having exactly the same chars and however the order of
chars are different.

Example 1:

String 1 = ABCD

String 2: BDCA

The String 1 and String 2 are anagrams

Example 2:

String 1: Race

String 2: Care

String 1 and String 2 are anagrams

Solution:

import [Link].*;

public class Main {

public static void main(String[] args) {

String string1 = "race";

String string2 = "care";

boolean isAnagram = false;

if ([Link]() != [Link]()) {

isAnagram = false;

else{
char[] char1 = [Link]();

char[] char2 = [Link]();

[Link](char1);

[Link](char2);

for (int i = 0; i < [Link]; i++) {

if (char1[i] != char2[i]) {

isAnagram = false;

break;

isAnagram = true;

if (isAnagram)

[Link](string1 + " and "+string2+ " are Anagram");

else

[Link](string1 + " and "+string2+ " are Not Anagram");

Output:

race and care are Anagram

6. Write a program to create a deadlock in java

Deadlock is the situation when a first thread is waiting for an object, that is acquired by a
second thread and the second thread is waiting for an object lock that is acquired by the first
thread. Since both threads are waiting for each other to release the lock. This condition is
called deadlock.

Solution:

class Main {

public static void main(String[] args) {

Object resource1 = new Object();


Object resource2 = new Object();

Thread t1 = new Thread() {

public void run() {

synchronized (resource1) {

[Link]("Thread 1: locked resource 1");

try {

[Link](100);

} catch (Exception e) {

synchronized (resource2) {

[Link]("Thread 1: locked resource 2");

};

Thread t2 = new Thread() {

public void run() {

synchronized (resource2) {

[Link]("Thread 2: locked resource 2");

try {

[Link](100);

} catch (Exception e) {

synchronized (resource1) {

[Link]("Thread 2: locked resource 1");

}
};

[Link]();

[Link]();

Output:

Thread 1: locked resource 1

Thread 2: locked resource 2

7. Consider I have data like below distance to the city from your hometown

Bangalore, 560

Chennai, 890

Hyderabad, 566

Mumbai, 788

New Delhi, 1000

Write a program that stores the above data in Map, and print them back by iterating over
each element.

Solution:

import [Link];

import [Link];

import [Link];

class Main {

public static void main(String[] args) {

Map<String,Integer> map = new HashMap<String,Integer>();

[Link]("Bangalore", 560);

[Link]("Chennai", 890);

[Link]("Hyderabad", 566);

[Link]("Mumbai", 788);

[Link]("New Delhi", 1000);


for(Entry<String,Integer> entry:[Link]()) {

[Link]("Distance from Hometown to " + [Link]()+" is " +


[Link]()+" Kms");

Output:

Distance from Hometown to New Delhi is 1000 Kms

Distance from Hometown to Chennai is 890 Kms

Distance from Hometown to Mumbai is 788 Kms

Distance from Hometown to Hyderabad is 566 Kms

Distance from Hometown to Bangalore is 560 Kms

You might also like