You are reading the article How Does Alias Work In Java With Programming Examples updated in November 2023 on the website Cancandonuts.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested December 2023 How Does Alias Work In Java With Programming Examples
Introduction to Java aliasIn Java, when multiple references are used to refer to the same object, it is commonly referred to as “alias.”The issue with aliasing is when a user writes to a particular object, and the owner, for several other references, does not expect that object to change. Here, the code that includes aliasing can get confusing fast, and it is very tedious to debug as well. Let us see how Java Alias works.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
How does an alias work in Java?
Let us take an example. x and y are two variable names that have two types, X and Y. Y extends X.
Y[] y = new Y[10]; X[] x = y; x[0] = new X(); Y[0].func1();In memory as well, they both point to the same location.
The memory location which is pointed is pointed by x as well as y. However, the actual object saved chooses which method to call during runtime.
Let us see another example.
Rectangle b1 = new Rectangle (0, 0, 50, 150); Rectangle b2 = b1;Both b1 and b2 refer to the same object, or we can say that the given object has two names, such as b1 and b2. Similar to a person that has two names, objects can also have two names.
When two aliased variables are present, changes that cause one variable also reflect on the other, as shown below.
System.out.println (b2.width); b1.grow (40, 40); System.out.println (b2.width);On executing the code, you will see that the changes that have been caused on one rectangle have occurred in the second rectangle as well. This is one of the main things that has to be noted for Alias in Java.
Examples of Java AliasThe following are some of the sample programs on Java Alias.
Example #1Code:
class X { public void func1() { System.out.println("called sample function 1"); } } class Y extends X { public void func1() { System.out.println("called sample function 1"); } public void func2() { System.out.println("called sample function 2"); } } public class AliasExample { public static void main(String[] args) { Y[] y = new Y[10]; X[] x = y; x[0] = new X(); y[0].func1(); } }Output:
How this occurs? What has to be changed? Is it possible to solve this?
Yes!! The only reason for this exception is that Java manages aliases during runtime. Only during the run time will it be able to know that the first one should be an object of Y instead of X.
class X { public void func1() { System.out.println("called sample function 1"); } } class Y extends X { public void func1() { System.out.println("called sample function 1"); } public void func2() { System.out.println("called sample function 2"); } } public class AliasExample { public static void main(String[] args) { Y[] y = new Y[10]; X[] x = y; x[0] = new Y(); y[0].func1(); } }Output:
Example #2Code:
public class AliasExample { public static void main(String[] args) { int a= 87; int b=87; System.out.println(a == b); b=a; System.out.println(a == b); } }What will happen if two arrays, a and b, are used instead of integer variables?
public class AliasExample { public static void main(String[] args) { int []a = {81, 54, 83}; int []b = {81, 54, 83}; System.out.println(a == b); b=a; System.out.println(a == b); } }Output:
In this program, two arrays, a and b, are created in the first step. Then, similar to the above program, a and b check whether they are equal. On executing the code, it can be seen that the output for the first check is false, and the output for the second check is true. It is because Java Alias works.
ConclusionThe drawback of alias is when a user writes to a specific object, and the owner, for some other references, does not guess that object to change.
Recommended ArticlesThis is a guide to Java Alias. Here we discuss how alias works in java along with programming examples for understanding better. You may also have a look at the following articles to learn more –
You're reading How Does Alias Work In Java With Programming Examples
How Reduce Function Work In Scala With Examples
Introduction to Scala reduce Function
Scala reduces functions to reduce the collection data structure in Scala. This function can be applied for both mutable and immutable collection data structures. Mutable objects are those whose values change frequently, whereas immutable objects are those objects that one assign cannot change itself. Reduce function can be applied to map, sequence, set, list, etc. This function returns a single value from the collection data structure. In reducing function, it merges all the values from the collection data structure and returns on a single value.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax
Below is the syntax of Scala reduce:
Explanation: In the above syntax, we have prepared one collection followed by the reduced function. It takes two parameters on which we can perform our operation. At last, we will obtain one value as output.
How does reduce Function work in Scala?Reduce function reduces the number of elements in the collection data structure. What it does internally is it uses binary operation and merges all the available elements into one single element. We can see how it works internally. Suppose we have one list of elements like 3, 7, 9, 2, 1, 5. Now we will apply to reduce function to generate a single number after adding all the numbers from the list.
1. First, it will take the first element 3. Then it will apply a binary operation on the first and second numbers and merge them to generate the third number.
Now we can have a look at one practical example for beginners for more understanding:
Code:
object Main extends App{ val collection = List(200 , 400, 100, 30, 1000, 500) }Explanation: In this example, we create one collection data structure list. After that, we apply to reduce function on the list to try to find out the max element from the list of elements. This will also return as a single value from the list of elements.
Extended class:
AbstractIterable[(K, V)]
Map[K, V]
Some of the supertype:
Map
Equals
MapFactoryDefaults
MapOps
PartialFunction
AbstractIterable
Iterable
IterableFactoryDefault
IterableOps
IterableOnceOps
IterableOnceOps
Some known classes:
TrieMap
AbstractMap
HashMap
IntMap
ListMap
Longman
Map1
Map2
Map3
Map4
withDefault
TreeMap
TrrSeqMap
VectorMap
AbstractMap
AnyRefMap
CollisionProofHashMap
LinkedHashMap
LongMap
WithDefault
SystemProperties
ListMap
OpenHashMap
reduceLeft
reduceRight
reception
reduceRightOPtion
reduceLeftOPtion
Examples to Implement Scala reduceBelow are the examples mentioned :
Example #1In this example, we find out the sum of all elements present in the collection data structure using a binary operation.
Code:
object Main extends App{ val list1 = List(200 , 400, 100, 30, 1000, 500) println("list before ::") println(list1) println("list after ::") println(result) }Output:
Example #2In this example, we find out the subtraction of all elements present in the collection data structure using a binary operation.
Code:
object Main extends App{ val list1 = List(200 , 400, 100, 30, 1000, 500) println("list before ::") println(list1) println("list after ::") println(result) }Output:
Example #3In this example, we are finding out the multiplication of all elements present into the collection data structure by using a binary operation.
Code:
object Main extends App{ val list1 = List(200 , 400, 100, 30, 1000, 500) println("list before ::") println(list1) println("list after ::") println(result) } Example #4In this example, we find out the division of all elements present in the collection data structure using a binary operation.
Code:
object Main extends App{ val list1 = List(200 , 400, 100, 30, 1000, 500) println("list before ::") println(list1) println("list after ::") println(result) }Output:
Example #5In this example, we are finding out the minimum of all elements present in the collection data structure by using a binary operation.
Code:
object Main extends App{ val list1 = List(200 , 400, 100, 30, 1000, 500) println("list before ::") println(list1) println("list after ::") println(result) }Output:
ConclusionScala reduces function is used to perform the binary operation on the collection elements. We can perform so many types of operations. Also, we can find the maximum and minimum values from the collection using the reduce function. Always remember this reduction will always return a single value as the output of the logic because it will merge all the values from the collection.
Recommended ArticlesWe hope that this EDUCBA information on “Scala reduce” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
How Does Offshoring Work With Example?
What is Offshoring?
“Offshoring” refers to relocating business processes, like manufacturing or supporting activities, from one country to another. Typically, a company opts for offshoring due to various operational benefits, such as a lower-cost labor market, skilled labor, presence in a foreign country, favorable tax regime, etc.
How Does Offshoring Work?Start Your Free Investment Banking Course
Download Corporate Valuation, Investment Banking, Accounting, CFA Calculator & others
Examples of OffshoringNow, let us look at some real-life examples currently available today.
1. WhatsAppIn 2009, WhatsApp offshored its software development unit to Eastern Europe, relying on the offshore unit to maintain the backstage, provide design solutions, and take care of main application development. It allowed WhatsApp’s in-house team to focus on client-related activities (say customer support) and solve other relevant business challenges.
2. AmazonIn 2023, Amazon employed an offshoring route for expansion wherein it acquired the startup Ring, which had an R&D set up in Ukraine. The acquired entity specialized in designing outdoor home security solutions, and this expertise boosted Amazon’s market position in the security system industry. Within a year, Amazon opened another offshore unit in the country to expand its market dominance.
3. People.ai Types of OffshoringThe offshoring process can be divided into two broad categories as follows:
1. Production OffshoringIn this type, a company sets up its manufacturing unit in another country, imports the finished goods, and then sells them in the domestic market. The company intends to leverage the availability of rich sources of raw materials and cheap labor to optimize its operational capability. For instance, a company may decide to set up the manufacturing unit of heavy machinery in a country with an adequate supply of iron and cheap local labor skilled in such a task.
2. Services OffshoringIn this type, a company relocates its services by setting up units in another country to carry out operations such as marketing, customer care, human resource, accounting, etc. In this case, the company employs a highly skilled workforce relatively cheaply in a foreign country. For instance, a software company may relocate its R&D unit to a country where the human resources possess strong technical knowledge but are comparatively cheaper than the domestic workforce.
Benefits of Offshoring
The parent company can focus more on its business’s core functions.
It allows companies to hire competent and cost-efficient labor from developing nations.
It ensures that a dedicated team works in the offshore unit and completes the assignment efficiently.
Many developing nations offer incentives to lure foreign companies’ investments, such as tax holidays.
It results in diversified operations across multiple countries lowering the country’s risk to a large extent.
A company entering a developing country owing to offshoring may find it difficult to communicate because of a huge gap between their native language and the local language of the foreign country.
Offshore units are geographically dispersed in distant locations, making it difficult for the management to control the subsidiaries.
Being acquainted with the local culture and society may result in various socio-cultural issues.
The offshore units may face threats of lockout or closure if the local people find the operations unethical or harmful to the nation’s identity.
Key TakeawaysSome of the key takeaways of the article are:
Offshoring refers to the relocation of business processes from one country to another. These business processes usually include manufacturing processes and various other supporting activities.
Once the offshoring set-up is established, the parent company can pay more attention to the business’s core operations.
There are two broad categories – production offshoring and services offshoring.
One of the major shortcomings of offshoring is the huge gap between the local language of the foreign country and the native language of the parent company, which results in miscommunication in many cases.
Conclusion Recommended ArticlesHow Does Predatory Lending Work With Example?
What is Predatory Lending?
Start Your Free Investment Banking Course
Download Corporate Valuation, Investment Banking, Accounting, CFA Calculator & others
How Does Predatory Lending Work?Predatory lenders usually use unscrupulous practices to entice and mislead borrowers toward taking out loans that they will eventually be unable to pay back or will be paying back at an extremely high cost. These lenders lure customers through aggressive sales tactics, often e-mail, phone calls, and even door-to-door.
Predatory lenders are least bothered putting so many borrowers at risk. Typically, predatory lending targets people who suffer from inadequate income, seeking money regularly to make ends meet. Especially borrowers with low credit scores and less educated fall prey to these discriminatory lending practices.
Examples of Predatory LendingHe later realized that he couldn’t afford the loan’s monthly payments. Eventually, he had no other option but to default on the loan, and the lender seized the property for foreclosure. Unfortunately, David lost his home, while the lender didn’t incur any losses as the home value exceeded the outstanding loan amount at the time of default. This is a classic example of predatory lending.
Types of Predatory LendingPredatory lending takes many different forms, and below are some of the most common types:
Flipping Loans: In a flipping loan arrangement, the lender repeatedly refinances a high-cost loan at a slightly lower interest rate. In the process, the lender earns through loan origination fees and broker fees, while the borrower incurs losses due to prepayment penalties and closing costs.
Balloon Payments: In this type of loan, the lender tries to sell a loan in which the payments are low initially, but a large payout becomes due at the end of its tenure. This large payment is popularly known as the balloon payment, which usually puts the borrower in a difficult financial position.
Negative Amortization: In the case of negative amortization, the borrower pays only a certain portion of the interest cost every month while the remaining interest cost gets added to the loan balance. So, the loan balance keeps growing and hence the name.
Payday Loans: A short-term loan becomes due on the next payday. It is one of the most dangerous forms of predatory lending owing to very high-interest rates. In a few cases, the annual percentage rate runs into three digits.
Title loans: Another short-term and high-interest rate predatory loan backed by collateral. If the borrower fails to repay the loan in full, the lender takes possession of the asset offered as collateral.
Warning signs for Predatory Lending
Basic costs are unclear: Although lenders are legally mandated to disclose all their costs upfront, predatory lenders usually twist the costs or make them hard to understand or find.
Too good to be true: These lenders often promise to help borrowers settle their debts easily, fix their already tainted credit history, or offer loans despite their pathetic credit history.
Existing complaints against the lender: It is essential to research the lender’s background before borrowing. If there are too many bad reviews or complaints, avoiding such lenders is better.
The lender doesn’t check creditworthiness: These lenders often don’t perform proper credit checks before lending money as they plan to pack on high-interest rates and insanely high upfront fees.
Key TakeawaysSome of the key takeaways of the article are:
Predatory lending refers to unfair and abusive lending practices lenders use to lure borrowers into loans with high interest rates and processing fees.
These lenders usually resort to aggressive sales tactics and deceive the borrowers into going for loans they can’t afford.
The predatory loans target less educated and vulnerable sections of society, wherein people struggle to meet monthly expenses or have recently lost their jobs.
Common predatory loans include flipping, balloon payments, negative amortization, payday, and title loans.
These loans are often too good to be true, with unclear basic costs, blemished lending history, and many more.
ConclusionSo, it can be seen that predatory lending usually employs methods that are not considered illegal, but they ruin the financial position of the borrowers. Although the regulators take many steps to prevent predatory lending, it won’t be possible without widespread awareness. The borrowers must understand the predator’s actions to avoid such traps. In short, the borrowers must read all the terms and conditions of the loan agreement before signing the contract. If they don’t understand anything in the loan contracts, it is better to avoid signing them.
Recommended ArticlesThis is a guide to Predatory Lending. Here we also discuss the definition, working, examples, types, and warning signs of predatory lending. You may also have a look at the following articles to learn more –
How Does Recourse Debt Work With Example?
What is Recourse Debt?
Download Corporate Valuation, Investment Banking, Accounting, CFA Calculator & others
The thing that is different in the case of recourse debt is that if the market value of the collateral is less than the outstanding loan balance, the lender has the right to seize the borrower’s other assets to make up for the remaining losses.
Features of Recourse Debt
Since it is backed by collateral, it is considered a secured loan.
The terms and conditions of the loan are very flexible, given it comes with collateral.
In the case of a default, the lender has the right to seize the asset offered as collateral.
The lender also has the right to go for other borrower assets if the collateral doesn’t cover the outstanding loan balance.
How does Recourse Debt work?The main feature of a recourse debt comes into play only when the borrower fails to honor the scheduled debt repayment, i.e., the borrower defaults. Then, the borrower is personally liable for the loan balance remaining after foreclosure of the asset provided as collateral. The lender can drag the borrower to the court to secure the pending payment using one of the following methods:
Garnishment: The lender can pay off the pending recourse loan by taking money straight from the payments coming into the borrower’s account or business.
Collections: The lender may consider selling the pending loan amount to a collection agency to collect the remaining money from the borrower.
Levies: The lender can legally claim the borrower’s other assets not provided initially as collateral in the loan agreement.
Examples of Recourse DebtThe following example will help us understand the concept of recourse debt.
David is a US resident who recently got a stable job in a real estate company with a monthly salary of $10,000. He plans to purchase a house in New York City for $300,000. David saved $75,000, which he will use as equity in this purchase. So, he has to go for a house finance loan of $225,000 from a financial institution.
Let us assume that David defaulted on the debt payment the following month, and the lender seized the house for foreclosure, resulting in a debt recovery of $175,000. So, the remaining $50,000 (= $225,000 – $175,000) plus the interest cost will be recovered by directly deducting the amount from the monthly paycheck until the outstanding loan balance is settled.
This is how a typical recourse debt works in the case of a default.
Recourse Debt vs. Non-Recourse LoanA non-recourse loan is also a secured form of financing, but it inherently differs from a recourse debt. Unlike recourse debt, the lender can only seize the collateral offered in the loan agreement if the borrower defaults on a non-recourse loan. This means that the lender must write off any outstanding balance after the foreclosure. The primary difference between the two debt forms is the lack of personal liability in a non-recourse loan in the case of a default.
Advantages of Recourse Debt
Since it is backed by collateral and the borrower’s liability, the lender has to bear a relatively lower risk than any other form of lending.
Borrowers with good credit profiles can get these loans at relatively favorable interest rates.
Borrowers with poor credit profiles can also get these loans as they are perceived to be low-risk by the lenders owing to unlimited liability borne by the borrower.
In this type of loan arrangement, if the borrower defaults on the loan repayment, the lender can claim the borrower’s income stream to recover the pending loan amount.
Sometimes borrowers become victims of predatory loan financing, where the lenders extend funds despite knowing imminent default owing to borrowers’ financial position.
Key TakeawaysSome of the key takeaways of the article are:
Recourse debt is a loan type in which the borrower defaults; the lender can seize the collateral and other assets to recover the money.
Other assets the lender may seize include income sources, deposit accounts, etc.
The primary difference between a recourse debt and a non-recourse loan is that the borrower is personally liable for the debt in the former.
It is backed by collateral and the borrower’s liability, so the lender has to bear a relatively lower risk than any other form of lending.
Conclusion Recommended ArticlesList Of Golang Constants With Programming Examples
Introduction to Golang Constants
Constants in go are the variable that once assigned the value and initialize we cannot change it during the process of the running of the program life cycle. The main importance of using the constant in go language allows us to define a variable which will be fixed throughout the compilation of the program. There are broadly three types of constant in the go language they are string constants (containing string value), numeric constants (containing an integer and floating values), Boolean constant (containing true and false value). In general programming, the constants are defined at the top of the program and will be available for the whole flow of the code and its value will be the same for the cycle. In this topic, we are going to learn about Golang Constants.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
List of Constants in Golang 1. Integer Constant
Integer type constants are the constant which contains the integer value.
In any integer type constant, they contain an integer part.
We can use the == operator for comparing two integer type constants.
We can use the = operator for assignment of any integer type constant.
Example for an integer constant
In the below example we have taken two variables X and Y and these variables contain two integer numbers. Here we are performing two operations inside the example, one we are adding them printing the resultant output of them, second we are comparing these two variables and printing the output of comparison of these two integer values.
Please see the below example along with the screen of output
Code:
package main import "fmt" func main() { const X= 3 var Y = 6 var sum = X+Y fmt.Println(sum)//printing the sum of the two numbers fmt.Println(X == 3) fmt.Println(Y < X) }Output:
2. Floating Type Constant
Floating type constants are the constant which contains the decimal value integer.
In any floating type constant they contain two parts one is the faction part value and another is the exponent part value.
We can use the == operator for comparing two floating type constants.
We can use the = operator for assignment of any floating type constant.
At the time of displaying the floating type constant we need to consider the decimal points.
Example of Floating Type constant
In the below example we have taken two variables X and Y and these variables contain two floating numbers. Here we are performing two operations inside the example, one we are adding them printing the resultant output of them, second we are comparing these two variables and printing the output of comparison of these two floating values.
Please see the below example along with the screen of output
Code:
package main import "fmt" func main() { const X= 3.4 var Y = 6.9 var sum = X+Y fmt.Println(sum) fmt.Println(X == 3.2)//It will print true or false fmt.Println(Y < X) } 3. String Constants
In Go language we can use two types of the constants one is with the single quote(‘’) and another with the double quote(“”).
We can perform all the operations on the string like string concatenation, to perform string concatenation in the language we can use + symbol. We can also use a format like +=.
Many times you will see the requirement of comparing the two strings in go language, so we can use the == operator for comparison of string.
We can simply use the = operator for operating on assignment.
Example for String Type constant
In the below example we have taken two variables X and Y and these variables contain two string values. Here we are performing two operations inside the example, one we concatenate them printing the resultant output of them, second, we are comparing these two variables and printing the output of comparison of these two strings values.
Please see the below example along with the screen of output
Code:
package main import "fmt" func main() { const X= "Ranjan" var Y = "Ajay" var greetings = X+ " " + Y greetings += "!" fmt.Println(greetings) fmt.Println(X == "Ranjan") fmt.Println(Y < X) }Output:
4. Boolean constant
The boolean constants are very much similar to any string constants.
The main difference between string constants and boolean constants is they allow us to define two types true and false whereas in case of the string constant they are only string.
Boolean constants are used for managing some flag values like yes or no or true or false.
Example for Boolean Type constant
In the below example we are performing some operations on a boolean value, we are assigning and printing the boolean value in the go language.
Please see the below example along with the screen of output
Code:
package main import "fmt" const Py = 3.14 func main() { const boolConst = true type exampleBool bool var initialBool = boolConst var simpleBool exampleBool = boolConst fmt.Println(initialBool) fmt.Println(simpleBool) }Output:
ConclusionFrom this tutorial we learned about the basic concept of the constant in the go language and we learned about its various types. We learned with the help of the various examples of the various types of constants in the go language.
Recommended ArticlesThis is a guide to Golang Constants. Here we discuss the List of Constants in Golang with programming examples for better understanding. You may also have a look at the following articles to learn more –
Update the detailed information about How Does Alias Work In Java With Programming Examples on the Cancandonuts.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!