1. Home
  2. Explore
  3. Computer Science
  4. Java Programming Fundamentals

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Java Programming Fundamentals

Transcript

Hi! My name is Mosh and I'm gonna be your instructor in this Java course. In this course, you're gonna learn everything you need to get started programming in Java. We'll start off by installing all the necessary tools to build Java applications then you're gonna learn about the basics of Java you'll learn how Java code gets executed you'll learn how to build simple algorithms and throughout this course I'm gonna share with you lots of tips and shortcuts from my years of experience I'll teach you how to write good code like a professional developer so we'll end up watching this course you will have a solid foundation in Java and be ready to learn about advanced Java features I've designed this course for anyone who wants to learn Java if you're a beginner don't worry I'll make Java super simple and hold your hands through this entire course you're not too old or too young you'll write your first Java program in

minutes my name is mosh I'm a software engineer with two decades of experience and I've taught over 3 million people how to code and how to become professional software engineers I have a coding school at code with mass comm where you can find plenty of courses that help you take her coding skills to the next level I hope you'll stick around and learn this beautiful and powerful programming language and now award from this video sponsor as someone who runs an online business I cannot stress enough the importance of staying safe online which is why I was so excited when dashlane reached out to me if you don't know - Lane is the password manager and VPN recommended by Apple and Google and it's a fantastic safeguard for keeping your information secure it's completely free to use for your first device so head over to - Ling comm / marché - give it a go if you want to upgrade to the premium to get VPN or

dark web monitoring you can use the promo code mosh to get 10% off sign up for - 9 today and keep yourself safe online now back to the course in this java tutorial we're going to download and install the necessary tools to build java applications so open up your browser and search for jdk download jdk is short for Java development kit and it's basically a software development environment for building Java applications it has a compiler it has a bunch of code that we can reuse it has a Java Runtime environment at a bunch of other stuff so over here you can see this page on Oracle com Java se

which is short for Java standard edition click on this now over here click on this icon now on this page we can see Java development kit for various platforms like Linux Mac OS and Windows here I'm on a Mac so I'm gonna download this dmg file over here now before we do this first we need to accept the license agreement all right now let's download the dmg let me open this we're gonna say this package let's double click this and here we see this installation wizard

it's super easy just click continue and install you have to enter your computer's password and then alright done beautiful so we can move this to trash now the next piece of software we need is a code editor there are so many cool editors for building Java applications the popular ones are NetBeans Eclipse and IntelliJ in this Java course I'm gonna use IntelliJ but if you have a favorite editor feel free to use that to take this course that's perfectly fine so let's search for IntelliJ download all right you can see download IntelliJ IDEA click on this link over here download the community edition which is absolutely free and it's more than enough for this course so download all right now let's drag and drop this onto the Applications folder beautiful alright we've installed all the necessary tools to build Java applications so next we're gonna look at the anatomy of a Java program

in this java tutorial we're gonna look at the anatomy of java programs the smallest building block in java programs are functions if function is a block of code that performs a task as a metaphor think of the buttons on the remote control of your TV each button performs a task functions in programming languages are exactly the same for example we can have a function for sending emails to people we can have a function for converting someone's weight in pounds to kilograms we can have a function for validating users input and so on now let's see how we can code a function in Java we start by specifying the return type of that function some functions return a value like a number at day time and so on other functions don't return anything so the return type of this functions is void void is a reserved keyword in Java and that's why I've coded that in blue here now after the return type we have

the name of our function so here we should give our function a proper descriptive name like send email this name clearly identifies the purpose of this function okay now after the name we have a pair of parentheses and inside these parentheses we add the parameters for this function we use these parameters to pass values to our function for example our send email function should have parameters like who is the receiver what is the subject of this email what is the content of this email and so on now in this tutorial we're not gonna worry about parameters we'll look at them in the future now after the parentheses we had a pair of curly braces and inside these braces we write the actual Java code now one thing I want you to pay attention to here is that in Java we put the left brace on the same line where we define our function in other programming languages like C sharp it's more

conventional to put the left brace on a new line but we don't do that in Java so we put the left brace on the same line where we define our function now every Java program should have at least one function and that function is called main so main is the entry point to our programs whenever we execute a Java program the main function gets called and the code inside this function gets executed okay now these functions don't exist on their own they should always belong to a class so a class is a container for one or more related functions basically we use these classes to organize our code just like how products are organized in a supermarket in a supermarket we have various sections like vegetables fruits cleaning products and so on each section contains related products by the same token a class in java contains related functions now every Java program should have at least one class that contains

the main function can you guess the name of that class its main so this is how we define a class in Java we start with a class keyword then we give our class a proper descriptive name and then we add a pair of curly braces now the functions that we define in between these curly braces belong to this class and more accurately we refer to them as methods so a method is a function that is part of a class in some programming languages like Python we can have a function that

exists outside of a class so we call it a function but when a function belongs to a class we refer to it as a method of that class okay now in Java all these classes and methods should have an access modifier an access modifier is a special keyword that determines if other classes and methods in this program can access these classes and methods we have various access modifiers like public private and so on now most of the time we use the public access modifier so we put that in front of our class and Method declarations so this is the basic structure of a Java program at a minimum we have a main class and inside this main class we have the main method now you might be curious why we have a capital m in the name of this class because in Java we use different conventions for naming our classes and our methods to name our classes we use the Pascal naming convention and that basically means the first letter of

every word should be uppercase in contrast to name our methods we use the camel naming convention and that means the first letter of every word should be operate case except the first wart so that is why we have a capital m in the name of this class alright now that you understand the anatomy of a Java program let's create a new Java project and see all these building blocks in action in this Java tutorial you're gonna learn

how to write and execute your first Java program so let's open IntelliJ IDEA here on the home screen let's create a new project alright on the left side select java and make sure project sdk is not black so earlier we downloaded jdk or Java development kit version 12 that is why JDK version 12 is selected here if you don't see that make sure to select it from this drop-down list alright now let's click on next on this page select create project from template so we're gonna create a command line application which is an application that we can run from the command line it doesn't have a graphical user interface or a GUI now I know command line application is not as exciting as an application with a graphical user interface like a mobile app or a desktop app but trust me building an application with a graphical user interface is very complicated so for now we're just gonna build command line applications to learn Java properly

once you learn Java properly then you can learn about building desktop or mobile applications with Java all right now let's click on next on this page we have to give our project a name let's call it hello world now over here you can see the location of this project so it's inside the idea projects folder now right below that you can see the base package which is set to comm that code with Marsh on my machine and your mission is probably gonna be comm dot package what is this well here we talked about classes and methods I told you that a class is a container for related methods so we use classes to organize our code by the same token we have a concept called package and we use a package to group related classes so as our applications grow we're gonna end up with hundreds or even thousands of classes so we should properly organize this class us into packages now by convention the

base package for a Java project is the domain name of your company in Reverse so my website is code with mass comm that is why I'm gonna set the base package for this project to come that code with Marsh now it doesn't mean that you should have an actual domain registered on an Internet this is just a way to create a namespace for our classes so now every class that we create in this project will belong to this package we're gonna talk about packages in more detail in the future so for now just type a base package for your project it can become that your name or whatever it doesn't really matter all right now let's go forward alright here's our first Java project now this code editor might look a little bit intimidating at first but trust me it's really easy and you're gonna learn about it throughout this course on the left side we have the project panel where we can see all the folders and

files in a project for example on the top we have the hello word project inside this project we have the source folder where we have the source code of a project now in this folder we have another folder that is calm that code with Marsh that is the name of our base package and in this package we have this class main so you can see this main file opened on the right side here now look at the name of this file its main the Java so all Java files should have the

Java extension okay now let's collapse the project panel by clicking on this icon that is better so see what we have here on top of this file we have the package statement and this is used to specify what package this class belongs to so the main class that we have here belongs to this package now this package statement is terminated by semicolon so in Java wherever we have a statement we should terminate that statement with a semicolon this is exactly like c-sharp or C++ now below this package statement we have our main class exactly like what you saw in the previous tutorial so we have public class main with a pair of curly braces inside this class we have our main method so it's a public method which may it's accessible from other parts of this program it's static we haven't talked about static metals yet we'll talk about them in the future for now just remember that the main method in your program

should always be static the return top of this method is void which means this method is not gonna return a value and here in parentheses we have one parameter for this function we can use these parameters to pass values to our program again we'll look at this in the future now right after this parameters as you can see the left brace and this is where we write the code in this method now by default we have this line prefix with two slashes this indicates a comment we use these comments to explain our code to other people so these comments don't get executed now let's remove this comment and write a bit of code to print something on the terminal so here we're gonna use the system class in Java so capital S system here in this tooltip you can see the system class is defined in this package Java dot Lang or language also look at this icon on the left side this indicates a class now inside this class we have various

members we can use the dot operator to see the members defined in the system class now the member that we're gonna access is out look at the icon of this member it's F which is short for field you're gonna talk about fields in the future when we talk about classes and object-oriented programming now what is interesting here is the type of this field and you can see that on the right side that is print string so print string is another class that is defined in Java so once again we use the dot operator to look at the methods or members defined in the print stream class the method we're gonna use is print Ln which is short for line look at the icon for this method so M indicates a method now you press ENTER and IntelliJ automatically adds these parentheses as well as a semicolon so now with the code on line six we're calling or executing the print line method earlier I told you that inside

this parenthesis we can pass values to our methods here we want to print the hello word on the terminal so let's type double quotes and inside these quotes right hello world so hello word is textual data in Java whenever we deal with textual data we should always surround them with double quotes now we say we have his string so a string is a sequence of characters all right so we're done with our first program now to execute this we can click on this icon on the toolbar look at the shortcut on

Mac it's ctrl + R I always prefer to use shortcuts because they're faster so ctrl + R now IntelliJ is building our application and we can see the result in this little terminal window so here's our hello work message so that was our first Java program next I'm going to explain how Java code gets executed under the hood hey Marsh here I just wanted to let you know that you really don't have to memorize anything in this course I've put together a comprehensive cheat sheet with summary notes that you can download below this video so I've done my best to create the best possible Java course and I would really appreciate it if you support me by liking and sharing this video on the social networks that you use often also be sure to subscribe and enable the notifications so next time I upload a video you'll get notified thank you so much and let's continue watching

all right now let's see what exactly happens under the hood the moment we run a Java program in IntelliJ there are basically two steps involved here compilation and execution in the compilation step IntelliJ uses the Java compiler to compile our code into a different format called Java bytecode this Java compiler comes with the Java development kit that we downloaded at the beginning of the course let me show you so here we can right click on this main the Java and in this context menu we have an item called open in terminal it's down below unfortunately it's not visible in my recording window it's called open in terminal on Mac and probably open in command prompt on Windows so let's open that we get this terminal window or command prompt on windows here we're currently inside of this folder code with Maj that is where we have our main the Java file now we can invoke the Java compiler like this

Java C and pass the name or Java file as an argument so main the Java if you're on Mac or Linux make sure to spell this with a capital M because these operating systems are case-sensitive so enter now let's look at the content of this folder on Mac or Linux we can type LS on windows we type dir so let's take a look in this folder now we have a new file main class this is the bytecode representation of this Java file now let me use IntelliJ to run our Java program

this class file gets stored somewhere else let me show you so back to the project panel here in our project we have this source folder where we our source code and we have this out folder where we have the result of the compilation so inside this folder we have production inside this we have hello world the same name as our project inside hello world we have comm which is the name of our top level package inside this package we have a sub package that is code with Marsh and here we have our main dat class file so this was the compilation step now this Java bytecode that we have in this file is platform independent and that means it can run on Windows Mac Linux or any operating systems that has a Java runtime environment if you go to java.com slash download we can download java or more accurately java runtime environment for various operating systems this Java Runtime environment has a software

component called Java Virtual Machine or JVM this JVM takes our Java bytecode and translates it to the native code for the underlying operating system so if you're on Windows machine this Java Virtual Machine converts or Java bytecode into the native code that windows can understand this architecture is the reason why Java applications are portable or platform independent we can write a Java program on a Windows machine and execute it on Linux Mac or

any other operating systems that have a Java runtime environment c-sharp and python also have the same architecture that's why they are platform independent as well now let me show you how to invoke this java virtual machine to run a Java program so back to this terminal window let me expand this currently we are inside of this folder code with Marsh and in this folder we have this class file now let's go one level up so CD dot dot and one more time

so now we are inside the source folder we can invoke Java Virtual Machine like this they type Java and then we type the full pass to our main class file what do I mean by that well earlier we defined this package comm that code with Marsh and this class the main class is part of this package so the full path this class is calm dot code with Marsh dot main make sure to use a capital M here because this is case sensitive now when we press ENTER Java will look at

this folder calm inside this folder it will look at this other folder code with Marsh and then it will find main that class in that folder it will load the bytecode and convert it to the native code for the operating system we are using so take a look so it executed our program hello world beautiful let me run a program using IntelliJ all these steps are hidden from us we don't see the compilation or execution steps so you have seen Java in action now let me tell you five interesting facts about

Java Java was developed by James Gosling in 1995 at Sun Microsystems which was later acquired by Oracle in 2010 it was originally called oke after an oak tree that stood outside Gosling's office later it was renamed to green and was finally renamed to Java inspired by Java coffee that's why its logo looks like this we have four editions of Java for building different kinds of applications we have Java standard edition this is the core Java platform which is what we're using in this course it contains all of the libraries that every Java developer must learn we have Java Enterprise Edition which is used for building very large-scale and distributed systems it's built on top of java standard edition and provides additional libraries for building fault tolerant distributed multi-tiered software we have java micro Edition which is a subset of Java standard edition designed for mobile devices so

it has libraries specific to mobile devices and finally we have Java card which is used in smart cards the latest version of Java is Java standard edition 12 which was released just a few months ago in March 2019 Java has close to 9 million developers worldwide currently about 3 billion mobile phones run Java as well as 120 million TV sets and every blu-ray player according to indeed.com the average salary of a Java developer is just over $100,000 per year in the US

so as we can see Java is everywhere which means more opportunities for you to get hired as a professional programmer now let me give you a quick overview of how I've structured this course so you can get the most out of it this course is the first part of my complete four-part Java series each part is about three to four hours long so it can easily complete it in a day or two in the first part which is what you're watching you're gonna learn the fundamentals of programming with Java in the next section you'll learn about the type system in Java you will learn how to work with various types such as purrs strings boolean's and arrays by the end of this section you will build a mortgage calculator as your first Java project will be improving this calculator bit by bit routers course next you will learn about control flow statements that are used to build algorithms we'll be talking about

various types of conditional statements and loops later in this section we'll add data validation to our mortgage calculator to force the user to enter valid values at this point you'll be able to build basic algorithms and that's great but being a good programmer requires knowing how to write good code code that is clean and expressive that's what separates an outstanding programmer from an average programmer so in the following section we'll talk about clean coding I will show you various techniques that professional programmers use to make their code clean and maintainable and finally in the last section you will learn how to find and fix errors in your java programs as well as how to package them for deployment so others can use them so the materials in the first part will give you a solid foundation on how to start programming in Java in the second part we'll talk about object oriented programming which is a style of programming use in most if

not all Java applications whether you want to use Java to build web mobile or desktop applications you need to understand object-oriented programming well because otherwise you're going to constantly hit obstacles in the third part we're going to talk about core Java API is or application programming interfaces you'll learn about many of the useful classes in the standard Java library and finally in the last part we'll be looking at the advanced features in Java such as streams threads database programming and so on so I hope you're gonna join me on this journey and master Java the most popular programming language behind millions of apps and websites you

intersection we're gonna look at the fundamentals of programming in Java you're gonna learn about variables and constants primitive and reference types you're gonna learn about casting or type conversion you will learn how to work with numbers strings and arrays and how to read input from the user once you learn all this I'm gonna give you a project you're gonna build a mortgage calculator on your own so make sure to pay great attention to all the materials you're gonna learn because you're gonna use most of them in this project are you ready now let's jump in and get started in this tutorial we're gonna talk about variables in Java we use variables to temporarily store data in computer's memory here is an example imagine in this program you want to store someone's age in the memory so we declare a variable like this ant age equals 30 so int or integer is the type of this variable so in this variable we

can only store integers which are whole numbers like 1 2 3 4 numbers that don't have a decimal point now in Java we have several different types I'm gonna talk about them in the next tutorial so first we specify the type of our variable then we give it a name or a label this is also called an identifier because we use it to identify our variable this equal sign is called the assignment operator and 30 is the initial value that we are assigning to this variable so we say on line 6 we're initializing this variable which means we're giving it an initial value you always have to initialize our variables before reading them so with this line we're storing number 30 somewhere in computer's memory and we're assigning this label to that memory location now on line 7 instead of printing hello world we can print the value of the age variable take a look so I'm gonna run this program using ctrl + R there you go now we see 30 on the

terminal beautiful we can also change the value of our variable so after we initialize it per house we can change it to 35 now when we run this program again we see it 35 beautiful we can also initialize multiple variables on the same line but this is something that I don't recommend because it makes your code ugly and hard to read here is an example we can declare another variable like temperature and set it to 20 so using a comma we can declare multiple

variables on the same line now even though this is technically possible it's not something that I recommend so it's always better to declare one variable on each line like this we can also copy the value of one variable into another here is an example let me delete these variables and declare a new variable called my age we set it to 30 and then we declare another variable like her age and we set it to my age so now when we print her age we're gonna see 30 take a

look so on line seven recapping the value of this variable into this other variable now one thing I want you to pay attention to here is the convention I have used for naming our variables as I told you before this is called the camelcase notation so we should capitalize the first letter of every word except the first word so in this case the first word my it's all in lowercase but the second word starts with a capital letter so this is all about declaring and initializing

variables in the next tutorial we're going to talk about various types in Java in this tutorial we're going to talk about various types in Java basically we have two categories of types we have primitive types for storing simple values and non primitive types or reference types for storing complex objects so in the category of primitive types we have white which takes one byte of memory and in one bite we can store values from 128 to 127 now

the more bytes we have the larger numbers we can store so next we have short which takes two bytes and with this we can store values up to 32,000 next we have integer which we have seen before integers take four bytes of memory and allow us to store values up to two billion then we have long which takes eight bytes and with this we can store even larger numbers now all these types are for storing whole numbers that don't have a decimal point if you want to store a number that has a decimal point you have to use float or double float takes four bytes double takes eight bytes so obviously we double we can store larger numbers next we have char for storing a single character like ABC and this chart I've take two bytes so they support international letters and finally we have boolean for storing boolean values which can be true or false just like yes or no in English now let's take a look at a few examples

earlier we use an integer for storing someone's age but as you learned integers take four bytes and allow us to store values up to two billion we don't need four bytes of memory to store someone's age all we need is one byte because with one bite we can store values up to 127 so I'm gonna change this to byte that is better now let's look at another example let's say we want to store the number of times a YouTube video has been watched so we define an integer called views count note that I'm always using meaningful names for my variables because these names help us understand what this code does I've seen some people use variable names like V or V 1 or n nobody knows what these variables do so as a best practice always use meaningful and descriptive names for your variables so views count we set this to a large number like 1 2 3 4 5 6 7 8 9 now in Java whenever you deal with a large

number like this you can use an underscore to separate every three digits just like how we use a comma in our documents to make our numbers more readable we can use an underscore in Java so with integers we can store values up to two billion but let's say the number of times this video has been watched is three billion so I had a three here now we have a red on the line that indicates an error let me hover our mouse over it we see this tooltip integer number too large so we need to change the type of this variable to long however the error is still there what's going on here the reason we're getting this error is that by default Java sees these numbers as integers so even though we have defined the type of this variable as long Java compiler sees this value as an integer and he thinks this value is too large for an integer to solve this problem we need to add an L as a suffix or this

number we can use an uppercase or a lowercase L but as you can see a lowercase L kind of looks like a 1 so it's better to use a capital L so these are examples of whole numbers now let's declare a variable for storing a number with a decimal point so double price we set this to 1099 obviously the double variable is too large for storing the price of a product so we can change this to float that is better but you have a compilation error here take a look

incompatible types required float but found double the reason we're seeing this error is that by default Java sees these numbers with a decimal point as double so even though we set the type of this variable to float Java sees this number as a double so just like how we added a suffix to this number to represent it as a long we need to add a suffix here to represent this number as a float and that suffix is F once again we can use an upper case or lower case F

so these are examples of numbers now let's store a character so char we call it letter and we set it to a note that we should always surround single characters with single quotes and multiple characters or strings with double quotes okay so char represents only one character string represents a series of characters and finally let's see an example of a boolean so we define a boolean variable called is eligible is this person eligible for loan or not we said this to true or false these are the

boolean values now note that all these words coded in orange are reserved keywords in Java just like public static void class package these are all reserved keywords so we cannot use these reserved keywords the name our variables classes and methods in the last tutorial you learned that we use primitive types to store simple values like numbers boolean values or single characters in contrast use reference types to store complex

objects like data objects or mail messages these are complex objects now in Java we have eight primitive types that you have seen before all the other types are reference types let me show you an example so here in this program first I'm gonna declare a primitive type let's say white age equals 30 now declaring and initializing a reference type is slightly different from primitive type let me show you so let's type date now here in this

tooltip box which is called intellisense we can see various classes that have date in their name so IntelliJ is helping us complete our code by suggesting these class names now here we have a date class in this package Java the util so this package contains a lot of utility classes that are useful in a lot of programs you also have a date class in a different package Java SQL or sequel which is used for programming databases so this is the benefit of packages we can have the same class but in different packages they don't conflict so packages create a namespace for our classes okay now in this case if we select the first date class and press enter or tab IntelliJ automatically adds this line for us import Java that you till the date so because currently we are in this package in order to use a class from a different package we need to import it so here we're importing the date class in this package will talk

about packages in more detail in the future so back to our date variable let's give this variable a name collect now now we set this here we need to use the new operator to allocate memory for this variable and this is one of the differences between the primitive and reference types when declaring primitive types we don't need to allocate memory memory is allocated and released by Java Runtime environment but when dealing with reference types we should always allocate memory now we don't have to release this memory Java Runtime environment will automatically take care of that so we use the new operator and then repeat the name of our class in this case date and then we add parentheses followed by a semicolon in this example this variable we have defined here is an instance of the date class so this class is defined template or blueprints for creating new objects new instances as another example we can

have a class called human and we can have objects like John Bob Mary and so on so an object is an instance of a class now this object or this class have members that we can access using the dot operator so we can type now dot and these are all the members defined in this class or in this object for example we have a method called get time and this returns the time component of this object this is another difference between primitive types and reference types these primitive types don't have members so if you type age dot we don't see anything these items you see here are not members of age their code snippets which allow us to quickly generate code for example we can select for I and this automatically generates this block of code for us we'll talk about this in the future so this age variable is a primitive type it's not an object it doesn't have any members and that's why when we use the dot operator

we don't see anything here now let's delete this line and instead print the value of this data object so once again we can type system this is a class so we can use the dot operator to access its members here we have out which is a field and the type of this field is print string which is another class in Java so once again we can use a dot operator and call the print line function now let me show you a very cool shortcut instead of typing all this we can use one of these code snippets so we type s oh you see and press tab and this generates this piece of code for us all right now let's pass our data object here note that I have not surrounded this variable with double quotes because this is a string and if you run this program we'll see now on the terminal there you go we don't want this you want the value of our data object not a label so let's remove the quotes I run the program

again so here's the current date I'm on my machine I've learned a little bit about the differences between the primitive and reference types so you know that we use primitive types for storing simple values and reference types for storing complex objects but there's a very important difference between these two categories of types in terms of memory management and that's what we're going to talk about in this tutorial so I'm going to declare a primitive variable X and set it to 1 and then I'm

going to declare another variable like Y and set it to X so in this example we have two different variables x and y and these two variables are at different memory locations so they're completely independent of each other in other words if I change the value of X Y is not going to get affected let me show you so I'm gonna update X to 2 and then print Y so syu t tab y let's take a look so run as you can see Y is not affected because x and y are completely

independent of each other however when we use a reference time this behavior is different let's take a look so I'm gonna delete all the code here in Java we have a point class that is defined in this package Java that awt so we press ENTER and now we have this import statement on the top beautiful let's declare a variable like point 1 and set it to new point here we can pass the initial values for x and y so I'm gonna pass 1

and 1 so intelligent automatically adds these labels x and y now just like before I'm gonna declare another variable point 2 and set it to point 1 and this is where things get interesting when Java Runtime environment executes line 8 first it's going to allocate some memory to store this point object let's see if the address of that memory location is 100 then it's going to allocate a separate part of the memory and it's going to attach this label to

that memory location point 1 in that memory location it's going to store the address of our point object so this is the critical difference between primitive and reference types when we declare a primitive variable like a byte the value that we assigned to that variable will be stored in that memory location but when we use a reference type like this point class our variable is going to hold the ax of that point object in memory not the actual pointer object now look at line 9 here we're copying the value that we have in this variable into this other variable so that value as you learn is not the point object is the address or the reference to the point object in memory that is why we refer to these tiles as reference types because they don't store the actual values they store a reference to an object somewhere in the memory so in this example point one and point two are referencing the exact

same point object in memory we only have one point object so these two variables are not independent of each other they're referencing the same object and that means if I update this point object through either of these variables the changes will be visible to the other variable I'm gonna show you so using the first variable point one we're going to update the value of x so we use the dot operator and here we can see the members of this object X on Y are both fields

which are variables that exist inside of a class so we said X just like a regular variable to a different value let's say two now because point one and point two are referencing the exact same object if we print point two we're going to see the change that we just made take a look so S or ut tab let's print point to run the program there you go so the change was visible so remember this reference types are copied by the references whereas primitive types are copied by their value and these values are completely independent of each other

in this tutorial we're gonna look at strings in Java so earlier in the course we printed the hello world message on a terminal this hello word that we have here is a string or more accurately it's a string literal that means a string value now let's extract this from here and store it in a string variable so cut just before this line we type string now look this string class is defined in Java that Lang package what is interesting is that we

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent