{"id":59444,"date":"2024-09-20T11:30:00","date_gmt":"2024-09-20T06:00:00","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=59444"},"modified":"2025-10-21T15:49:10","modified_gmt":"2025-10-21T10:19:10","slug":"getting-started-with-java","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/getting-started-with-java\/","title":{"rendered":"Getting Started with Java: Essential Concepts and Techniques"},"content":{"rendered":"\n<p>Java is a powerful programming language that is one of the main pillars of full-stack development. If you are starting out as a full-stack developer, it is important that you know about this language. <\/p>\n\n\n\n<p>In case if you don&#8217;t, worry not, this article is there to help you out and will cover the foundational aspects of Java in detail. So, without further ado, let us get started!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Basic Terminologies in Java<\/strong><\/h2>\n\n\n\n<p>When we consider a <a href=\"https:\/\/www.guvi.in\/blog\/java-programs-for-freshers\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java program<\/a>, it can be defined as a collection of objects that communicate via invoking each other&#8217;s methods. Let us now briefly look into what do class, object, methods, and instance variables mean.<\/p>\n\n\n\n<ul>\n<li><strong>Class:<\/strong> The class is a blueprint (plan) of the instance of a class (object). It can be defined as a logical template that share common properties and methods.<\/li>\n<\/ul>\n\n\n\n<p>Example 1: Blueprint of the house is class.<br>Example 2: In the real world, Alice is an object of the \u201cHuman\u201d class.<\/p>\n\n\n\n<ul>\n<li><strong>Object:<\/strong>&nbsp; Objects have states and behaviors. Example: A dog has states &#8211; color, name, breed as well as behavior such as wagging their tail, barking, and eating. An object is an instance of a class.<\/li>\n<\/ul>\n\n\n\n<ul>\n<li><strong>Methods :<\/strong> A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.<\/li>\n<\/ul>\n\n\n\n<ul>\n<li><strong>Instance Variables :<\/strong> Each object has its unique set of instance variables. An object&#8217;s state is created by the values assigned to these instance variables.<\/li>\n<\/ul>\n\n\n\n<ul>\n<li><strong>public static void main(String [] args):<\/strong> The method main() is the main entry point into a Java program; this is where the processing starts. Also allowed is the signature&nbsp;<\/li>\n<\/ul>\n\n\n\n<p><strong>public static void main(String\u2026 args){ ..}<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Public Class \u00a0 class_name {\npublic static void main(String&#91;] args)\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"welcome to HCL GUVI\");\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/code><\/pre>\n\n\n\n<p>&nbsp; <strong>&nbsp;Output &#8211;<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>welcome to HCL GUVI<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Comments in Java<\/strong><\/h3>\n\n\n\n<p>There are three types of comments in Java.&nbsp;<\/p>\n\n\n\n<ul>\n<li><strong>Single line Comment<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\/\/ <\/strong>System.out.println(\"This is an comment.\");<\/code><\/pre>\n\n\n\n<ul>\n<li><strong>Multiline Comment<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\/*<\/strong>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"This is the first line comment.\");&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"This is the second line comment.\");<strong>*\/<\/strong><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Identifiers in java :<\/strong><\/h3>\n\n\n\n<p><strong>&nbsp;<\/strong>All Java components require names. Names used for classes, variables, and methods are called identifiers.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXdRrB_0rkqruvAakAQUiXPmrg3nz7KvcITqFYVe4wg6D05svTPTlTiyHFgACb9KSC6n1kSbCAYeZygzvTps1TT_D-ZzTy8uh4l-PyACbVtpAeNe5EnJpdHdV_FR5GgX2dK51Pe5yOLklpK-d8Qq8IZakOmNtXupl5lR-P8YGExvIufQRQcrMw?key=54X4JSqCb0hCWp9Uuj_IZQ\" alt=\"java identifier\" title=\"\"><\/figure>\n\n\n\n<p><strong>Rules to write<\/strong> <strong>Identifiers :<\/strong><\/p>\n\n\n\n<ul>\n<li>All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).<\/li>\n\n\n\n<li>After the first character, identifiers can have any combination of characters.<\/li>\n\n\n\n<li>A keyword cannot be used as an identifier.<\/li>\n\n\n\n<li>We can\u2019t use space in between an identifier.<\/li>\n\n\n\n<li>Most importantly, identifiers are case sensitive.<\/li>\n\n\n\n<li>Examples of legal identifiers: age, $salary, _value, __1_value.<\/li>\n\n\n\n<li>Examples of illegal identifiers: 123abc, -salary<strong>.<\/strong><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Keywords in Java:<\/strong><\/h3>\n\n\n\n<p>Keywords are predefined, reserved words used in Java programming that have special meanings to the compiler. The following list shows the reserved words in Java. These reserved words may not be used as constant or variable or any other identifier names.<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXdSjUn9SA7qa4K2r8lNdqZbKnfp5I-ENmaBwYPT1tjjFiNxDtyhkOUynoIot7o0NpTFSMVBFqHNZfXHJoRDfibpUU2dZNjXF9SdCj_U5ZgAGnIobdQdAm9Co9jtcwVPBU7pzey7nu1WSplbnqLRIalU9oq2UxBQIRACQ2sVSQ6H9vgWQMuL-0g?key=54X4JSqCb0hCWp9Uuj_IZQ\" width=\"686\" height=\"298\" alt=\"\" title=\"\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What are Data Types in Java?<\/strong><\/h2>\n\n\n\n<p>Data types in <a href=\"https:\/\/www.guvi.in\/blog\/introduction-to-java\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java<\/a> are of different sizes and values that can be stored in the variable that is made as per convenience and circumstances to cover up all test cases. Java has two categories in which data types are segregated<\/p>\n\n\n\n<ol>\n<li><strong>Primitive Data Type:<\/strong> such as Boolean, char, int, short, byte, long, float, and&nbsp; &nbsp; &nbsp; &nbsp; double<\/li>\n<\/ol>\n\n\n\n<ol start=\"2\">\n<li><strong>Non-Primitive Data Type or Object Data type:<\/strong> such as String, Array, etc.<\/li>\n<\/ol>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXdMSz5cVPl20ROsdClEJOLzmc9ADarUwUDhatSpdG1rFQDzWv0feKcvf5NRVL5yTnkNHNpbUcmX2rG61Sg216BJ1kdcdZraEQS_-jABmQxiGtQychEldotz0rBmtXve9kqglvcg1AqZ_6IHfsKZFPDo4vMLihL64Y0_VcfSap5tbB4JWYqGyHA?key=54X4JSqCb0hCWp9Uuj_IZQ\" style=\"\" alt=\"\" title=\"\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Primitive Data Types in Java<\/strong><\/h3>\n\n\n\n<p>Primitive data are only single values and have no special capabilities.&nbsp; There are 8 primitive data types. They are depicted below in tabular format below as follows:<img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXeX3WRouB_htNDlHqRGylrHpinlpj-sdKiJIR8Hj60s3Ymcz2ojH9FfYbCbXF6ncvsLNVm0OmAje90qu5il160UMorulHEOENWaV2F7afQTrqRHvHY4ECYBwWZFyWFAoZI0WY6RRuzkiF1HkPm0zEZyZA6AnjXGsV_HNIh4jvAjbJbZHopFLg?key=54X4JSqCb0hCWp9Uuj_IZQ\" width=\"709\" height=\"423\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>Let us discuss and implement each one of the following data types that are as follows:<\/p>\n\n\n\n<p>1. <strong>Boolean Data Type<\/strong><\/p>\n\n\n\n<p>Boolean data type represents only one bit of information either <strong>true or false<\/strong> which is intended to represent the two truth values of logic and Boolean algebra, but the size of the Boolean data type is virtual machine-dependent. Values of type Boolean are not converted implicitly or explicitly (with casts) to any other type. But the programmer can easily write conversion code.<\/p>\n\n\n\n<p><strong>Syntax<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>boolean<\/strong> booleanVar;<\/code><\/pre>\n\n\n\n<p><strong>Size<\/strong>: Virtual machine dependent.<\/p>\n\n\n\n<p><strong>2. Byte Data Type<\/strong><\/p>\n\n\n\n<p>The byte data type is an 8-bit signed two\u2019s complement integer. The byte data type is useful for saving memory in large arrays.<\/p>\n\n\n\n<p><strong>Syntax:&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>byte<\/strong> byteVar;<\/code><\/pre>\n\n\n\n<p><strong>Size:<\/strong> 1 byte (8 bits)<\/p>\n\n\n\n<p><strong>3. Short Data Type<\/strong><\/p>\n\n\n\n<p>The short data type is a 16-bit signed two\u2019s complement integer. Similar to byte, use a short to save memory in large arrays, in situations where the memory savings actually matters.<\/p>\n\n\n\n<p><strong>Syntax:&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> <strong>short<\/strong> shortVar;<\/code><\/pre>\n\n\n\n<p><strong>Size:<\/strong> 2 bytes (16 bits)<\/p>\n\n\n\n<p><strong>4. Integer Data Type<\/strong><\/p>\n\n\n\n<p>It is a 32-bit signed two\u2019s complement integer.<\/p>\n\n\n\n<p><strong>Syntax:&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>int<\/strong> intVar;<\/code><\/pre>\n\n\n\n<p><strong>Size:<\/strong> 4 bytes ( 32 bits )<\/p>\n\n\n\n<p><strong>5. Long Data Type<\/strong><\/p>\n\n\n\n<p>&nbsp;The range of a long is quite large. The long data type is a 64-bit two\u2019s complement integer and is useful for those occasions where an int type is not large enough to hold the desired value. The size of the Long Datatype is 8 bytes (64 bits).<\/p>\n\n\n\n<p><strong>Syntax:&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>long<\/strong> longVar;<\/code><\/pre>\n\n\n\n<p><strong>6. Float Data Type<\/strong><\/p>\n\n\n\n<p>The float data type is a single-precision 32-bit IEEE 754 floating-point. Use a float (instead of double) if you need to save memory in large arrays of floating-point numbers. The size of the float data type is 4 bytes (32 bits).<\/p>\n\n\n\n<p><strong>Syntax:&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>float<\/strong> floatVar;<\/code><\/pre>\n\n\n\n<p><strong>7. Double Data Type<\/strong><\/p>\n\n\n\n<p>The double data type is a double-precision 64-bit IEEE 754 floating-point. For decimal values, this data type is generally the default choice. The size of the double data type is 8 bytes or 64 bits.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>double<\/strong> doubleVar;<\/code><\/pre>\n\n\n\n<p><strong>8. Char Data Type<\/strong><\/p>\n\n\n\n<p>The char data type is a single 16-bit Unicode character with the size of 2 bytes (16 bits).<\/p>\n\n\n\n<p><strong>Syntax:&nbsp;<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>char<\/strong> charVar;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Non-Primitive Data Type or Reference Data Types :<\/strong><\/h3>\n\n\n\n<p>The Reference Data Types will contain a memory address of variable values because the reference types won\u2019t store the variable value directly in memory. They are strings, objects, arrays, etc.&nbsp;<\/p>\n\n\n\n<p><strong>1. Strings&nbsp;<\/strong><\/p>\n\n\n\n<p>Strings are defined as an array of characters. The difference between a character array and a string in Java is, that the string is designed to hold a sequence of characters in a single variable whereas, a character array is a collection of separate char-type entities. Unlike C\/C++, Java strings are not terminated with a null character.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong> Declaring a string<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;String_Type&gt; &lt;string_variable&gt; = \u201c&lt;sequence_of_string&gt;\u201d;<\/code><\/pre>\n\n\n\n<p><strong>Example:&nbsp;<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ Declare String without using new operator\u00a0<br>String s = &#8220;welcome to hcl Guvi&#8221;;\u00a0<br><br>\/\/ Declare String using new operator\u00a0<br>String s1 = new String(&#8220;welcome to hcl guvi&#8221;);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul>\n<li><strong>Primitive data type vs. non-primitive type in Java<\/strong><\/li>\n<\/ul>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXe7L027eqWSeC76dX5FRIntZ_y8uMZG1J6b9Mbz7letICiVD9-wojQnNAIpq3Mz-FL4BCXEzTpENZlG1M0JOvP_bi_0YNpQGMBoZbt8c9-6qVMw8UHqOoxRpiPHjjUjlOx7LdSEdSfOYqQJEOZ_zHdbdTgjcfht4fePCz8sQmMzuleazQQ1teo?key=54X4JSqCb0hCWp9Uuj_IZQ\" width=\"596\" height=\"395\" alt=\"\" title=\"\"><\/p>\n\n\n\n<ul>\n<li><strong>Variables in Java<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Java variable is a name given to a memory location. It is the basic unit of storage in a program.<\/p>\n\n\n\n<ul>\n<li>The value stored in a variable can be changed during program execution.<\/li>\n\n\n\n<li>Variables in Java are only a name given to a memory location. All the operations done on the variable affect that memory location.<\/li>\n\n\n\n<li>In Java, all variables must be declared before use.<\/li>\n<\/ul>\n\n\n\n<ul>\n<li><strong>How to Declare Variables in Java?<\/strong><\/li>\n<\/ul>\n\n\n\n<p>We can <a href=\"https:\/\/www.javatpoint.com\/java-variable-declaration\" target=\"_blank\" rel=\"noreferrer noopener\">declare variables in Java<\/a> as pictorially depicted below as a visual aid.<img decoding=\"async\" width=\"511\" height=\"233\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXe8IxLxBxDSky3FyNwk6WtH0duVIwGyfeNDK9QTp6_2If8Jncfn4HUjOg8TXNanL734hlIbJl1H2LLdPSQcnazlQ-4K4HmjCZiVJyfFORGmIQZ4rV5p1hY4sZ8dvinxMNvmhqjOEL0eC3LxcGxKNejXwbKNWlEY9WtxPeDf9xbgJlQgIwQcEw?key=54X4JSqCb0hCWp9Uuj_IZQ\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>From the image, it can be easily perceived that while declaring a variable, we need to take care of two things that are:<\/p>\n\n\n\n<p><strong>datatype:<\/strong> Type of data that can be stored in this variable.&nbsp;<\/p>\n\n\n\n<p><strong>data_name<\/strong>: Name was given to the variable.<\/p>\n\n\n\n<ul>\n<li><strong>How to Initialize Variables in Java?<\/strong><\/li>\n<\/ul>\n\n\n\n<p>It can be perceived with the help of 3 components that are as follows:<\/p>\n\n\n\n<p><strong>datatype:<\/strong> Type of data that can be stored in this variable.<\/p>\n\n\n\n<p><strong>variable_name:<\/strong> Name given to the variable.<\/p>\n\n\n\n<p><strong>value:<\/strong> It is the initial value stored in the variable.<img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXe5tMzTW8F_SMgGC_o3hAt6QPXHdI9ueIj2_zK4pxvAHcsU5wkipHSVy4FivHH1I6ryI6v_P4N0ZNpWBSvOv6H4eNnvpk9MC0ZrQW0_Iw5UpUNqQ4bZ53UZUtzb0dca7C05DZ5Nn-SU5gFY1yaR4YALB2vsO3EZLkuOSGRPpBN0Ylr-ePcSQA?key=54X4JSqCb0hCWp9Uuj_IZQ\" width=\"324\" height=\"247\" alt=\"\" title=\"\"><\/p>\n\n\n\n<ul>\n<li><strong>Types of Variables in Java<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Now let us discuss different types of variables which are listed as follows:&nbsp;<\/p>\n\n\n\n<ul>\n<li>Local Variables<\/li>\n\n\n\n<li>Instance Variables<\/li>\n\n\n\n<li>Static Variables<\/li>\n<\/ul>\n\n\n\n<p><strong>1. Local Variables&nbsp;<\/strong><\/p>\n\n\n\n<p>A variable defined within a block or method or constructor is called a local variable.&nbsp;<\/p>\n\n\n\n<ul>\n<li>These variables are created when the block is entered, or the function is called and destroyed after exiting from the block or when the call returns from the function.<\/li>\n\n\n\n<li>The scope of these variables exists only within the block in which the variables are declared, i.e., we can access these variables only within that block.<\/li>\n\n\n\n<li>Initialization of the local variable is mandatory before using it in the defined scope.<\/li>\n<\/ul>\n\n\n\n<p>Below is the implementation of the above approach:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class hcl guvi {\n    public static void main(String&#91;] args)\n    {\n        \/\/ Declared a Local Variable\n        int var = 10;\n \n        \/\/ This variable is local to this main method only\n        System.out.println(\"Local Variable: \" + var);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>2. Instance Variables<\/strong><\/p>\n\n\n\n<p>Instance variables are non-static variables and are declared in a class outside of any method, constructor, or block.&nbsp;<\/p>\n\n\n\n<ul>\n<li>As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.<\/li>\n\n\n\n<li>Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier, then the default access specifier will be used.<\/li>\n\n\n\n<li>Initialization of an instance variable is not mandatory. Its default value is dependent on the data type of variable.&nbsp;<\/li>\n\n\n\n<li>Instance variables can be accessed only by creating objects.<\/li>\n\n\n\n<li>We initialize instance variables using constructors while creating an object. We can also use instance blocks to initialize the instance variables.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Java Program to demonstrate\nclass hcl guvi {\n\t\/\/ Declared instance variable\n\tString student = \"Shubham Jain\";\n\tpublic static void main(String&#91;] args)\n\t{\n                          hcl Guvi Zen=new hcl Guvi();\n\t\t\/\/ geek variable can be accessed by creating object\n\n\tSystem.out.print(Zen. student);  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>3. Static Variables<\/strong><\/p>\n\n\n\n<p>Static variables are also known as class variables.&nbsp;<\/p>\n\n\n\n<ul>\n<li>These variables are declared similarly to instance variables. The difference is that static variables are declared using the static keyword within a class outside of any method, constructor, or block.<\/li>\n\n\n\n<li>Unlike instance variables, we can only have one copy of a static variable per class, irrespective of how many objects we create.<\/li>\n\n\n\n<li>Static variables are created at the start of program execution and destroyed automatically when execution ends.<\/li>\n\n\n\n<li>Initialization of a static variable is not mandatory. Its default value is dependent on the data type of variable.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Java Program to demonstrate\nclass hcl guvi {\n\t\/\/ Declared instance variable\n\tPublic static String student = \"Shubham Jain\";\n\tpublic static void main(String&#91;] args)\n\t{\n                       \n\t\/\/ student variable can be accessed without object\n        \/\/ static variable\n\n\tSystem.out.print(hcl guvi. student);  }}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><\/pre>\n\n\n\n<ul>\n<li><strong>Scope of variables&nbsp;<\/strong><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXc_DuNeoDRFiYYPduCI1NqmXxXdqCtslUPdXKX4xHLWNRiWq1Rarmevr1oxqpMyke2SnwT0Id5v4MuZtp_oKKjsquA5fZSF0Hc-KgM_DYjeboCXWJt_M_6H5E7wzZc9XB5FLx0SWt-MmeRY5mYPp9mtciocwPaRFwq7AA54OGrMXILYchoTIw?key=54X4JSqCb0hCWp9Uuj_IZQ\" width=\"766\" height=\"161\" alt=\"\" title=\"\"><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Operators in Java<\/strong><\/h3>\n\n\n\n<p>Operators in Java are the symbols used for performing specific operations in Java. Operators make tasks like addition, multiplication, etc. which look easy although the implementation of these tasks is quite complex.<\/p>\n\n\n\n<p><strong>Types of Operators in Java :<\/strong><\/p>\n\n\n\n<ol>\n<li><strong>Arithmetic Operators<\/strong><\/li>\n<\/ol>\n\n\n\n<p>These operators involve the mathematical operators that can be used to perform various simple or advanced arithmetic operations on the primitive data types referred to as the operands. These operators consist of various unary and binary operators that can be applied on a single or two operands.&nbsp;<img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXdIf7qBxK_d9pZYGUCU2YiF2eRaA1j_m6XM3avaf-6fakI2EfugEN_T2PTQE_62ftdNB5CuE6OCHI7zeGJFmRDp9VvZoTKbIZTB5VQotnHp8jTgQu5daXycZEea3SXzjFEXeGWZNGahnkWzAliQjVXJ0T-WA02RselZguxOQM_ZsT0kYCYvN4s?key=54X4JSqCb0hCWp9Uuj_IZQ\" width=\"556\" height=\"313\" alt=\"\" title=\"\"><\/p>\n\n\n\n<ol start=\"2\">\n<li><strong>Unary Operators in Java<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Java unary operators are the types that need only one operand to perform any operation like increment, decrement, negation, etc. It consists of various arithmetic, logical and other operators that operate on a single operand. Let\u2019s look at the various unary operators in detail and see how they operate.<br><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfIrIKijTiUR54C6AhpMs9bbo7w_GQce87hFRutXuFVBXTGanGzAcVNe7Sd6B8pJKKGBBPmo79P6XV_V5A8oXVqXo26s9D8M6Jt7zxER191sqT-bp7_UZCJ9xotLnfw2Eb6x1rnsXu8CYiWFdp9HpCyFR3YWN2GGUuB1EBmP6vEMbv-hHb1u94?key=54X4JSqCb0hCWp9Uuj_IZQ\" width=\"630\" height=\"249\" alt=\"\" title=\"\"><\/p>\n\n\n\n<ol start=\"3\">\n<li><strong>Assignment Operators<\/strong><\/li>\n<\/ol>\n\n\n\n<p>These operators are used to assign values to a variable. The left side operand of the assignment operator is a variable, and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type of the operand on the left side. Otherwise, the compiler will raise an error. This means that the assignment operators have right to left associativity.<br><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXc2LFN3OYgKW2VSlVa7BxJvOAbybeF5V1pZGbQywi1pBaEzSFGjFmbxtCoKrEzzpguPaXSohKDfkpZRwhm9FCkwhTP-JOj-bn2PdlhKRgoEyJAez0la9_nP7BwwcQZtUb7djdTcV_2jhcKwQnuruT3u8eM92j2nGWJp7PJ-iIVdqxRoyNetq30?key=54X4JSqCb0hCWp9Uuj_IZQ\" width=\"465\" height=\"328\" alt=\"\" title=\"\"><\/p>\n\n\n\n<ol start=\"4\">\n<li><strong>Relational Operators<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Java Relational Operators are a bunch of binary operators used to check for relations between two operands, including equality, greater than, less than, etc. They return a boolean result after the comparison and are extensively used in looping statements as well as conditional if-else statements and so on.<br><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfwIrcv_WtbcSLoZl_chbdxfKwty2uTJ0LwfETKa-yTC4YHC69Ljwf_8PJrODao3q-UnrVgNwnDrkWdF7Pz6-SE7BXz0i7FBVbDQoKZMy_-WfxPFgDgeY8FIlb8dQTYIrk83rwSv-CIIB56kiBjPqlAlo-ID8_wQe8n1e3oDrHMxwVFFtT_fg?key=54X4JSqCb0hCWp9Uuj_IZQ\" width=\"598\" height=\"203\" alt=\"\" title=\"\"><\/p>\n\n\n\n<ol start=\"5\">\n<li><strong>Logical Operators<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Logical operators are used to perform logical \u201cAND\u201d, \u201cOR\u201d and \u201cNOT\u201d operations, i.e. the function similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions\/constraints or to complement the evaluation of the original condition under particular consideration. One thing to keep in mind is, while using AND operator, the second condition is not evaluated if the first one is false. Whereas while using OR operator, the second condition is not evaluated if the first one is true<\/p>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXeHlNtr8tb5rYpSElNbTIHX-DMkXrxS_i_jfWlGyqTgS9br5123QusG2xE7A-FjKczSGVuh4Figt1F4ojAUN2Q59fMn8oTdLWgp8phQsABnFGjmAydLntqHahlbboulCMpo8Rn6Gc6j0CSj0Sj30GsVBQqDB2r72qXUQRp9TLs4GIrPrOJj1dM?key=54X4JSqCb0hCWp9Uuj_IZQ\" width=\"406\" height=\"351\" alt=\"\" title=\"\"><\/p>\n\n\n\n<ol start=\"6\">\n<li><strong>Ternary Operator<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Java ternary operator is the only conditional operator that takes three operands. It\u2019s a one-liner replacement for the if-then-else statement and is used a lot in Java programming. We can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary operators.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>variable = Expression1 ?&nbsp; Expression2 : Expression3<\/code><\/pre>\n\n\n\n<p><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXf-r29Lcnq7c2oMkREHB1vcnI7d09loN90Q-iEBKEifKjFx82ua3OLKqJwyfhQmVSLK8o_DrRqcynGOM-Iy9dM8C45mqiYzDUL_388ypGZZ5cKDVq3ex8-NTy6yeF6bMAFdM-DJJ4LBpnyzWhtbeCUkgU0u81KxnRj65VnGSUrvorxNH3tAmQ?key=54X4JSqCb0hCWp9Uuj_IZQ\" width=\"427\" height=\"540\" alt=\"\" title=\"\"><\/p>\n\n\n\n<ul>\n<li><strong>&nbsp;&nbsp;<\/strong><strong>Type Casting in Java :<\/strong><\/li>\n<\/ul>\n\n\n\n<p>In Java, type casting is a method or process that converts a data type into another data type in both ways manually and automatically. The automatic conversion is done by the compiler and manual conversion is performed by the programmer. In this section, we will discuss type casting and its types with proper examples.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXcPzxf-OcVv4IcR05dp6B6_sKQ3f6D44xfBv1jSVr3hy2f1himoVdv9OKDAT6v0FRhBM3R--fJtc8D8GtXnCo8KqtH4jlJVbnUAFb7sQhD9J5TKS-n-CGTSEOjAQmGNqibKij_UOEAvwoTz-cUMuPL5tdQK8lgncQYYgFV40jygYeG5xYA7Gm4?key=54X4JSqCb0hCWp9Uuj_IZQ\" alt=\"Type Casting in Java\" title=\"\"><\/figure>\n\n\n\n<p><strong>\u00b7<\/strong><strong> &nbsp; &nbsp; &nbsp; <\/strong><strong>Type casting<\/strong><\/p>\n\n\n\n<p>Converting a value from one data type to another data type is known as type casting.&nbsp;<\/p>\n\n\n\n<p>Types of Type Casting<\/p>\n\n\n\n<p>There are two types of type casting:<\/p>\n\n\n\n<p>1. Widening Type Casting<\/p>\n\n\n\n<p>2. Narrowing Type Casting<\/p>\n\n\n\n<ul>\n<li>&nbsp;<strong>&nbsp;&nbsp;<\/strong><strong>Widening Type Casting<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Converting a lower data type into a higher one is called widening type casting. It is also known as implicit conversion or casting down. It is done automatically. It is safe because there is no chance to lose data. It takes place when:<\/p>\n\n\n\n<p>o &nbsp; Both data types must be compatible with each other.<\/p>\n\n\n\n<p>o &nbsp; The target type must be larger than the source type<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>byte -&gt; short -&gt; char -&gt; int -&gt; long -&gt; float -&gt; double&nbsp;<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>For example, the conversion between numeric data types to char or Boolean is not done automatically. Also, the char and Boolean data types are not compatible with each other. Let&#8217;s see an example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class WideningTypeCastingExample  \n{  \npublic static void main(String&#91;] args)  \n{  \nint x = 7;  \n\/\/automatically converts the integer type into long type  \nlong y = x;  \n\/\/automatically converts the long type into float type  \nfloat z = y;  \nSystem.out.println(\"Before conversion, int value \"+x);  \nSystem.out.println(\"After conversion, long value \"+y);  \nSystem.out.println(\"After conversion, float value \"+z);  \n}  \n}  \n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Before conversion, the value is: 7<br>After conversion, the long value is: 7<br>After conversion, the float value is: 7.0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul>\n<li><strong>&nbsp;&nbsp;<\/strong><strong>Narrowing Type Casting<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Converting a higher data type into a lower one is called narrowing type casting. It is also known as explicit conversion or casting up. It is done manually by the programmer. If we do not perform casting then the compiler reports a compile-time error.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>double -&gt; float -&gt; long -&gt; int -&gt; char -&gt; short -&gt; byte&nbsp;<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong><\/strong>Let&#8217;s see an example of narrowing type casting.<\/p>\n\n\n\n<p>In the following example, we performed the narrowing type casting two times. First, we have converted the double type into a long data type after that long data type is converted into an int type.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class NarrowingTypeCastingExample  \n{  \npublic static void main(String args&#91;])  \n{  \ndouble d = 166.66;  \n\/\/converting double data type into long data type  \nlong l = (long)d;  \n\/\/converting long data type into int data type  \nint i = (int)l;  \nSystem.out.println(\"Before conversion: \"+d);  \n\/\/fractional part lost  \nSystem.out.println(\"After conversion into long type: \"+l);  \n\/\/fractional part lost  \nSystem.out.println(\"After conversion into int type: \"+i);  \n}  \n}  \n&nbsp;&nbsp;<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Before conversion: 166.66<br>After conversion into long type: 166<br>After conversion into int type: 166<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>In case, you want to learn more about Java and gain in-depth knowledge on full-stack development, consider enrolling for HCL GUVI&#8217;s certified <a href=\"https:\/\/www.guvi.in\/zen-class\/java-full-stack-development-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=getting-started-with-java\" target=\"_blank\" rel=\"noreferrer noopener\">Java Full-stack Developer Course<\/a> that teaches you everything from scratch and make sure you master it!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In conclusion, Java is a versatile and foundational language in full-stack development, with its core concepts such as classes, objects, methods, and variables forming the building blocks of programming.<\/p>\n\n\n\n<p>Understanding the various data types, operators, and type-casting mechanisms is crucial for writing efficient Java code. <\/p>\n\n\n\n<p>Mastering these basics will provide a strong foundation for further exploration into more advanced Java programming and full-stack development, enabling you to build robust and scalable applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java is a powerful programming language that is one of the main pillars of full-stack development. If you are starting out as a full-stack developer, it is important that you know about this language. In case if you don&#8217;t, worry not, this article is there to help you out and will cover the foundational aspects [&hellip;]<\/p>\n","protected":false},"author":40,"featured_media":64333,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[294,720],"tags":[],"views":"6084","authorinfo":{"name":"Lavish Jain","url":"https:\/\/www.guvi.in\/blog\/author\/lavish-jain\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/09\/getting_started_with_java_1x-300x116.webp","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2024\/09\/getting_started_with_java_1x.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/59444"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=59444"}],"version-history":[{"count":7,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/59444\/revisions"}],"predecessor-version":[{"id":90539,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/59444\/revisions\/90539"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/64333"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=59444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=59444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=59444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}