<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4242766661380116441</id><updated>2026-08-19T21:49:33.206-07:00</updated><category term="Core Java"/><category term="SQL"/><category term="Java 8"/><category term="JDBC"/><category term="Servlets"/><category term="Collection"/><category term="IOC"/><title type='text'>Code With Clarity</title><subtitle type='html'>Java Programming tutorials and Interview Questions.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.codewithclarity.org/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>35</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-5876782480847267668</id><published>2025-12-14T00:45:00.000-08:00</published><updated>2025-12-14T11:09:41.664-08:00</updated><title type='text'></title><content type='html'>&lt;!--MAIN CONTAINER--&gt;
&lt;div style=&quot;box-sizing: border-box; margin: auto; max-width: 900px; padding: 16px;&quot;&gt;

  &lt;!--HERO TITLE--&gt;
  &lt;h2 style=&quot;color: #d32f2f; font-weight: 700; line-height: 1.3; text-align: left;&quot;&gt;
    How to Master JAVA without wasting Time &amp;amp; Money in Learning C/C++
  &lt;/h2&gt;

  &lt;!--INTRO--&gt;
  &lt;p style=&quot;font-size: 16px; line-height: 1.7; margin-top: 24px;&quot;&gt;
    &lt;strong&gt;Most students are told:&lt;/strong&gt;&lt;br /&gt;
    👉 &lt;em&gt;“First learn C, then C++, then Java.”&lt;/em&gt;
  &lt;/p&gt;

  &lt;p style=&quot;font-size: 16px; line-height: 1.7;&quot;&gt;
    &lt;strong&gt;Truth?&lt;/strong&gt;&lt;br /&gt;
    That path wastes &lt;strong&gt;years&lt;/strong&gt;, confuses beginners, and kills confidence.
  &lt;/p&gt;

  &lt;p style=&quot;font-size: 16px; line-height: 1.7;&quot;&gt;
    If your goal is &lt;strong&gt;JAVA Developer / Backend / Spring Boot&lt;/strong&gt;,  
    you &lt;strong&gt;don’t need&lt;/strong&gt; C or C++.
  &lt;/p&gt;

  &lt;!--WHAT YOU NEED--&gt;
  &lt;h3 style=&quot;margin-top: 32px;&quot;&gt;What you actually need:&lt;/h3&gt;
    ✔ Core Java (OOP + Clarity)&lt;br /&gt;✔ Problem-solving in Java&lt;br /&gt;✔ Real project mindset&lt;br /&gt;✔ Industry-relevant learning path&lt;/div&gt;&lt;div style=&quot;box-sizing: border-box; margin: auto; max-width: 900px; padding: 16px;&quot;&gt;&lt;h3 style=&quot;margin-top: 32px;&quot;&gt;If you’re:&lt;/h3&gt;
  &lt;ul style=&quot;font-size: 16px; line-height: 1.8; padding-left: 20px;&quot;&gt;
    &lt;li&gt;Confused where to start&lt;/li&gt;
    &lt;li&gt;Stuck watching random YouTube videos&lt;/li&gt;
    &lt;li&gt;Spending money on courses but not progressing&lt;/li&gt;
  &lt;/ul&gt;

  &lt;!--CTA BUTTON--&gt;
  &lt;div style=&quot;margin: 36px 0px; text-align: center;&quot;&gt;
    &lt;a href=&quot;https://tinyurl.com/bdhejyd8&quot; style=&quot;background: rgb(31, 111, 235); border-radius: 10px; color: white; display: block; font-size: 18px; font-weight: 600; margin: auto; max-width: 420px; padding: 16px; text-decoration: none;&quot; target=&quot;_blank&quot;&gt;
      🚀 Book Your Spot!
    &lt;/a&gt;
  &lt;/div&gt;

  &lt;!--TESTIMONIALS--&gt;
  &lt;h2 style=&quot;margin-top: 50px; text-align: center;&quot;&gt;
    ⭐ What My Students Say ⭐
  &lt;/h2&gt;

  &lt;p style=&quot;color: #555555; text-align: center;&quot;&gt;
    Real feedback from students who built their Java careers with clarity.
  &lt;/p&gt;

  &lt;!--IMAGE GRID--&gt;
  &lt;div style=&quot;display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); margin-top: 24px;&quot;&gt;

    &lt;!--IMAGE ITEM (repeat safely)--&gt;
    

  &lt;img loading=&quot;lazy&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUSpo63-IO0q8WmUj99Rilh2vD3BcKigQpVRpjMQWqaVc035ZTPL4jmvzCypFx5G6wOIOkDdi3os7Wu40KP-4MlBy2oAjK23EFSw0887bH66TDmTkKnVry-Qfs8pojP-A7yQLQQDEAtJi7cLievbB2l-VnLNUxBS67fJPn7fyzsMMbap0wECr0FLBEy2tM/s640/Screenshot%202025-12-14%20142935.png&quot; style=&quot;border-radius: 8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 10px; width: 100%;&quot; /&gt;

  &lt;img loading=&quot;lazy&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIeliLNxqOhLJ_yhyarUAmt2wgy5kgAx_IQ_sdFv5kmip4SQBcHsttcYKOPUv5klwpfyl_sTp9X69rFTOew_22OyD7xpibcWxfB4il7Dl5N-0VWpN27kNmTDBOqxAKZ3Ob0lEZnA_G1xyi0wg0f7lBYeHQNLYJ1ZuzMMVBtLVSMOJAQ-F89cstTHzr8UhK/s640/Screenshot%202025-12-14%20143002.png&quot; style=&quot;border-radius: 8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 10px; width: 100%;&quot; /&gt;

  &lt;img loading=&quot;lazy&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVyJyYnZiTBactqIb1oW4JbA-TSOhz1B_90M2FfLoqJmf1wytsPJDN_Hr73e8nHxNG0PILXNEc8WjYMWuia_ORNtb0L3ul6rTym44edhLUvRp-R_RO0pDHVyv16RKqYWmXs8u1aMuVOWlbblMp-AnqC9y4YinIa7ylRBn10WteGZ9SXLp88PDk4b54DyYr/s640/Screenshot%202025-12-14%20143040.png&quot; style=&quot;border-radius: 8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 10px; width: 100%;&quot; /&gt;

  &lt;img loading=&quot;lazy&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqP838qj7KhT1u5X7zJCqe-KTiRNDoap6AICR5mq3UVB7WlTEoR3PYbyipg5u58udmrGLnze-CeJcqvDYyAb313B4RH1iXZHXNdg3EDpQjtjsHqFedy7rDy7lb5_mTMtJO_a4cBG7F91-_CVa2xv8CiXpoo5bSdM2ePIJq32HQhDqHNIQdTtWx1_6l8b2P/s640/Screenshot%202025-12-14%20143106.png&quot; style=&quot;border-radius: 8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 10px; width: 100%;&quot; /&gt;

  &lt;img loading=&quot;lazy&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg4JgprTFP7U8gnC2JHwbgvlZq-leubqvmz84NpCwsG9b2E72auYtWzE92pKxpWCffMdJixiYu3a4PVm5PfUPR2wd-P6RTv2E4ZYYUZGds6gIY0Cwu8cg7ieBelTd2f9f_QMvAAT27CyHHys_3CQZkJ1d9yET8OieXRgahtK6lBj5xAEIAsJfEQeb4DFNbI/s640/Screenshot%202025-12-14%20143133.png&quot; style=&quot;border-radius: 8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 10px; width: 100%;&quot; /&gt;

  &lt;img loading=&quot;lazy&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPeu57Knwxm0mcCWiOtbKEpzRft0GENVCfzPj64Flcp00_aq-Zi4gDsr9u7FMUo-w2Z0f324CgjEZMp2OMoDH4ppoFs7lgBEv1tJSvX7Lo5JKQOrbj1lz8R_Pv20sJqcVEgs4UKGnFtDLYK4iB2lxSsh2vBv8kHQrx3E_83rQ7XEX0qhm3PgO-G84ZO5lD/s640/Screenshot%202025-12-14%20143209.png&quot; style=&quot;border-radius: 8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 10px; width: 100%;&quot; /&gt;

  &lt;img loading=&quot;lazy&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdhI5s1YLyn2cLbC0AZkibNbFBPvM71yKHy3QeFGbRbE4MAuLxzbHHQps1zXjqmWVhtua0RwLL2OsIlyoV93XJ0H8eG7aln0j7yL7BeT-FwhtbIeppDvFpAeyQGDOfPj1W-LgDfYzTXgETfhXcNJLBpnK2IBusqJJgsxYFP2tkP1lbrtGx4SSTJ1Wyxzjn/s640/Screenshot%202025-12-14%20143242.png&quot; style=&quot;border-radius: 8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 10px; width: 100%;&quot; /&gt;

  &lt;img loading=&quot;lazy&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFdfI-XNLVnRdAEv9hy9dEXLzj_y-wvg93siuunbPy8cgwaCJ9IZU7AF8qXTp2yoCRJWfsa5cHVu3lo66ZIlVqRWjuRf9xZBMzG-qwIC-CAW6o5EFKtAgac3oGifTVjDvzO8yS24pD-f5eRNzJ4rFZpW9mK1C55RyZXfl7VFEFK6PXaHma5E6o9Evdh9Jo/s640/Screenshot%202025-12-14%20143306.png&quot; style=&quot;border-radius: 8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 10px; width: 100%;&quot; /&gt;

  &lt;img loading=&quot;lazy&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPS-C-KuZPcqXqJHxAgjNiqRLWg1JBRb8mWwuJuNlh8wfCeRbTpbIWFFTSn2vgsKmjlC22KZ89UoAvCC6eihG165GJXdg_y-DrL4xta1rc2BINv1P8gJnkZnuYoTvQXmZwTngjNyqaozXzjpGln0hfyQE80ToPjzfsmzedN96gnBuoSlVLa7TRgmeRHHcV/s640/Screenshot%202025-12-14%20143331.png&quot; style=&quot;border-radius: 8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 10px; width: 100%;&quot; /&gt;

  &lt;/div&gt;

  &lt;!--FINAL CTA--&gt;
  &lt;div style=&quot;margin: 40px 0px; text-align: center;&quot;&gt;
    &lt;a href=&quot;https://tinyurl.com/bdhejyd8&quot; style=&quot;background: rgb(31, 111, 235); border-radius: 10px; color: white; display: block; font-size: 18px; font-weight: 600; margin: auto; max-width: 420px; padding: 16px; text-decoration: none;&quot; target=&quot;_blank&quot;&gt;
      🚀 Book Your Spot!
    &lt;/a&gt;
  &lt;/div&gt;
      
&lt;!-- VIDEO TESTIMONIALS SECTION --&gt;
&lt;h2 style=&quot;text-align:center; margin-top:50px;&quot;&gt;
  🎥 Student Video Feedback
&lt;/h2&gt;

&lt;p style=&quot;text-align:center; color:#555;&quot;&gt;
  Hear directly from students who gained clarity and confidence.
&lt;/p&gt;

&lt;div style=&quot;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
  margin-top: 30px;
&quot;&gt;

  &lt;!-- VIDEO CARD 1 --&gt;
  &lt;div style=&quot;
    background:#ffffff;
    border-radius:12px;
    overflow:hidden;
    box-shadow:0 6px 14px rgba(0,0,0,0.08);
  &quot;&gt;
    &lt;div style=&quot;position:relative; padding-top:56.25%;&quot;&gt;
      &lt;iframe
        src=&quot;https://www.youtube.com/embed/XJLIuV85-94&quot;
        style=&quot;position:absolute; top:0; left:0; width:100%; height:100%;&quot;
        frameborder=&quot;0&quot;
        allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot;
        allowfullscreen&gt;
      &lt;/iframe&gt;
    &lt;/div&gt;
    &lt;div style=&quot;padding:14px; text-align:center; font-weight:500;&quot;&gt;
      Ambe Kalawati — Full-Stack Developer
    &lt;/div&gt;
  &lt;/div&gt;

  &lt;!-- VIDEO CARD 2 --&gt;
  &lt;div style=&quot;
    background:#ffffff;
    border-radius:12px;
    overflow:hidden;
    box-shadow:0 6px 14px rgba(0,0,0,0.08);
  &quot;&gt;
    &lt;div style=&quot;position:relative; padding-top:56.25%;&quot;&gt;
      &lt;iframe
        src=&quot;https://www.youtube.com/embed/nGgCfcjCDAo&quot;
        style=&quot;position:absolute; top:0; left:0; width:100%; height:100%;&quot;
        frameborder=&quot;0&quot;
        allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot;
        allowfullscreen&gt;
      &lt;/iframe&gt;
    &lt;/div&gt;
    &lt;div style=&quot;padding:14px; text-align:center; font-weight:500;&quot;&gt;
      Anjali — Full-Stack Developer
    &lt;/div&gt;
  &lt;/div&gt;
  
 &lt;!-- VIDEO CARD 3 --&gt;
&lt;div style=&quot;
  background:#ffffff;
  border-radius:12px;
  overflow:hidden;
  box-shadow:0 6px 14px rgba(0,0,0,0.08);
&quot;&gt;
  &lt;div style=&quot;position:relative; padding-top:56.25%;&quot;&gt;
    &lt;iframe
      src=&quot;https://www.youtube.com/embed/bMW9u4wrl50&quot;
      style=&quot;position:absolute; top:0; left:0; width:100%; height:100%;&quot;
      frameborder=&quot;0&quot;
      allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot;
      allowfullscreen&gt;
    &lt;/iframe&gt;
  &lt;/div&gt;
  &lt;div style=&quot;padding:14px; text-align:center; font-weight:500;&quot;&gt;
    Anu — Full-Stack Developer
  &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
  
        &lt;!--FINAL CTA--&gt;
  &lt;div style=&quot;margin: 40px 0px; text-align: center;&quot;&gt;
    &lt;a href=&quot;https://tinyurl.com/bdhejyd8&quot; style=&quot;background: rgb(31, 111, 235); border-radius: 10px; color: white; display: block; font-size: 18px; font-weight: 600; margin: auto; max-width: 420px; padding: 16px; text-decoration: none;&quot; target=&quot;_blank&quot;&gt;
      🚀 Book Your Spot!
    &lt;/a&gt;
  &lt;/div&gt;


&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/5876782480847267668'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/5876782480847267668'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2025/12/how-to-master-java-without-wasting-time.html' title=''/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUSpo63-IO0q8WmUj99Rilh2vD3BcKigQpVRpjMQWqaVc035ZTPL4jmvzCypFx5G6wOIOkDdi3os7Wu40KP-4MlBy2oAjK23EFSw0887bH66TDmTkKnVry-Qfs8pojP-A7yQLQQDEAtJi7cLievbB2l-VnLNUxBS67fJPn7fyzsMMbap0wECr0FLBEy2tM/s72-c/Screenshot%202025-12-14%20142935.png" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-1616904786129060924</id><published>2020-01-27T23:12:00.000-08:00</published><updated>2020-01-27T23:18:56.298-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>Trigger</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&amp;nbsp;In this lesson, we will introduce you to MySQL triggers.&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;By definition, a MySQL trigger is a type of stored program, associated with a table, that will be activated automatically once a specific event related to the table of association occurs.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;This event must be related to one of the following three DML statements: INSERT, UPDATE, or DELETE.&lt;/li&gt;
&lt;li&gt;Therefore, triggers are a powerful and handy tool that professionals love to use where database consistency and integrity are concerned.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;Moreover, to any of these DML statements, one of two types of triggers can be assigned – a &quot;&lt;b&gt;before&lt;/b&gt;&quot;, or an &quot;&lt;b&gt;after&lt;/b&gt;&quot; trigger. In other words, a trigger is a MySQL object that can “&lt;b&gt;trigger&lt;/b&gt;” a specific action or calculation ‘&lt;b&gt;before&lt;/b&gt;’ or ‘&lt;b&gt;after&lt;/b&gt;’ an INSERT, UPDATE or DELETE statement has been executed. For instance, a trigger can be activated before a new record is inserted into a table, or after a record has been updated.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Perfect! Let’s execute some code:&lt;/b&gt;&lt;br /&gt;
First, in case you are just starting Workbench, select “Employees” as your default database.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;USE employees;&lt;/b&gt;&lt;br /&gt;
Then, execute a COMMIT statement, because the triggers we are about to create will make some changes to the state of the data in our database. At the end of the exercise, we will ROLLBACK up to the moment of this COMMIT.&lt;br /&gt;
&lt;b&gt;COMMIT;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
We said triggers are a type of stored program. Well, one could say the syntax resembles that of stored procedures, couldn’t they?&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;BEFORE INSERT&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
DELIMITER $$&lt;br /&gt;
CREATE TRIGGER before_salaries_insert&lt;br /&gt;
BEFORE INSERT ON salaries&lt;br /&gt;
FOR EACH ROW&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;IF NEW.salary &amp;lt; 0 THEN&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;SET NEW.salary = 0;&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;END IF;&lt;br /&gt;
END $$&lt;br /&gt;
DELIMITER ;&lt;br /&gt;
After stating we want to CREATE a TRIGGER and then indicating its name, we must indicate its type and the name of the table to which it will be applied. In this case, we devised a “before” trigger, which will be activated whenever new data is inserted in the “&lt;b&gt;Salaries&lt;/b&gt;” table.&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;Then, an interesting phrase follows – “for each row”. It designates that before the trigger is activated, MySQL will perform a check for a change of the state of the data on all rows. In our case, a change in the data of the “Salaries” table will be caused by the insertion of a new record.&lt;br /&gt;
&lt;br /&gt;
Within the BEGIN-END block, you can see a piece of code that is easier to understand if you just read it without focusing on the syntax.&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;The body of this block acts as the core of the “before_salaries_insert” trigger. Basically, it says that if the newly inserted salary is of negative value, it will be set as 0.&lt;br /&gt;
/*&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;IF NEW.salary &amp;lt; 0 THEN&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;SET NEW.salary = 0;&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;END IF;&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;From a programmer’s perspective, there are three things to note about these three lines of code.&lt;br /&gt;
&amp;nbsp;First, especially for those of you who are familiar with some programming, this is an example of a conditional. The IF statement starts the conditional block. Then, if the condition for negative salary is satisfied, one must use the keyword THEN before showing what action should follow. The operation is terminated by the END IF phrase and a semi-colon.&lt;br /&gt;
&lt;br /&gt;
The second thing to be noted here is even more interesting. That’s the use of the keyword NEW. In general, it refers to a row that has just been inserted or updated. In our case, after we insert a new record, “NEW dot salary” will refer to the value that will be inserted in the “Salary” column of the “&lt;b&gt;Salaries&lt;/b&gt;” table.&lt;br /&gt;
The third part of the syntax regards the SET keyword. As you already know, it is used whenever a value has to be assigned to a certain variable. Here, the variable is the newly inserted salary, and the value to be assigned is 0.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Let’s execute this query. BEFORE INSERT&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
DELIMITER $$&lt;br /&gt;
CREATE TRIGGER before_salaries_insert&lt;br /&gt;
BEFORE INSERT ON salaries&lt;br /&gt;
FOR EACH ROW&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;IF NEW.salary &amp;lt; 0 THEN&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;SET NEW.salary = 0;&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;END IF;&lt;br /&gt;
END $$&lt;br /&gt;
DELIMITER ;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;Let’s check the values of the “Salaries” table for employee 10001.&lt;br /&gt;
SELECT&lt;br /&gt;
&amp;nbsp; &amp;nbsp; *&lt;br /&gt;
FROM&lt;br /&gt;
&amp;nbsp; &amp;nbsp; salaries&lt;br /&gt;
WHERE&lt;br /&gt;
&amp;nbsp; &amp;nbsp; emp_no = &#39;10001&#39;;&lt;br /&gt;
&amp;nbsp;Now, let’s insert a new entry for employee 10001, whose salary will be a negative number.&lt;br /&gt;
INSERT INTO salaries VALUES (&#39;10001&#39;, -92891, &#39;2010-06-22&#39;, &#39;9999-01-01&#39;);&lt;br /&gt;
&amp;nbsp;Let’s run the same SELECT query to see whether the newly created record has a salary of 0 dollars per year.&lt;br /&gt;
SELECT&lt;br /&gt;
&amp;nbsp; &amp;nbsp; *&lt;br /&gt;
FROM&lt;br /&gt;
&amp;nbsp; &amp;nbsp; salaries&lt;br /&gt;
WHERE&lt;br /&gt;
&amp;nbsp; &amp;nbsp; emp_no = &#39;10001&#39;;&lt;br /&gt;
You can see that the “before_salaries_insert” trigger was activated automatically. It corrected the value of minus 92,891 we tried to insert.&lt;br /&gt;
&amp;nbsp;Now, let’s look at a BEFORE UPDATE trigger. The code is similar to one of the triggers we created above, with two substantial differences.&lt;br /&gt;
&lt;b&gt;&lt;u&gt;BEFORE UPDATE&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
DELIMITER $$&lt;br /&gt;
CREATE TRIGGER trig_upd_salary&lt;br /&gt;
BEFORE UPDATE ON salaries&lt;br /&gt;
FOR EACH ROW&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;IF NEW.salary &amp;lt; 0 THEN&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;SET NEW.salary = OLD.salary;&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;END IF;&lt;br /&gt;
END $$&lt;br /&gt;
DELIMITER ;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;First, we indicated that this will be a BEFORE UPDATE trigger.&lt;br /&gt;
/*&lt;br /&gt;
BEFORE UPDATE ON salaries&lt;br /&gt;
*/&lt;br /&gt;
&amp;nbsp;Second, in the IF conditional statement, instead of setting the new value to 0, we are basically telling MySQL to keep the old value.&amp;nbsp; Technically, this is achieved by setting the NEW value in the “Salary” column to be equal to the OLD one. This is a good example of when the OLD keyword needs to be used.&lt;br /&gt;
/*&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;IF NEW.salary &amp;lt; 0 THEN&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;SET NEW.salary = OLD.salary;&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;END IF;&lt;br /&gt;
*/&lt;br /&gt;
&amp;nbsp;Create the “before_salaries_update” trigger by executing the above statement.&lt;br /&gt;
&lt;br /&gt;
Then, run the following UPDATE statement, with which we will modify the salary value of employee 10001 with another positive value.&lt;br /&gt;
UPDATE salaries&lt;br /&gt;
SET&lt;br /&gt;
&amp;nbsp; &amp;nbsp; salary = 98765&lt;br /&gt;
WHERE&lt;br /&gt;
&amp;nbsp; &amp;nbsp; emp_no = &#39;10001&#39;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; AND from_date = &#39;2010-06-22&#39;;&amp;nbsp; &amp;nbsp; &lt;br /&gt;
Execute the following SELECT statement to see that the record has been successfully updated.&lt;br /&gt;
SELECT&lt;br /&gt;
&amp;nbsp; &amp;nbsp; *&lt;br /&gt;
FROM&lt;br /&gt;
&amp;nbsp; &amp;nbsp; salaries&lt;br /&gt;
WHERE&lt;br /&gt;
&amp;nbsp; &amp;nbsp; emp_no = &#39;10001&#39;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; AND from_date = &#39;2010-06-22&#39;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp;Now, let’s run another UPDATE statement, with which we will try to modify the salary earned by 10001 with a negative value, minus 50,000.&lt;br /&gt;
UPDATE salaries&lt;br /&gt;
SET&lt;br /&gt;
&amp;nbsp; &amp;nbsp; salary = - 50000&lt;br /&gt;
WHERE&lt;br /&gt;
&amp;nbsp; &amp;nbsp; emp_no = &#39;10001&#39;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; AND from_date = &#39;2010-06-22&#39;;&amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp;Let’s run the same SELECT statement to check if the salary value was adjusted.&lt;br /&gt;
SELECT&lt;br /&gt;
&amp;nbsp; &amp;nbsp; *&lt;br /&gt;
FROM&lt;br /&gt;
&amp;nbsp; &amp;nbsp; salaries&lt;br /&gt;
WHERE&lt;br /&gt;
&amp;nbsp; &amp;nbsp; emp_no = &#39;10001&#39;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; AND from_date = &#39;2010-06-22&#39;;&amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp;No, it wasn’t. Everything remained intact. So, we can conclude that only an update with a salary higher than zero dollars per year would be implemented.&lt;br /&gt;
&amp;nbsp;All right. For the moment, you know you have created only two triggers. But how could you prove that to someone who is seeing your script for the first time? Well, in the ‘info’ section of the “employees” database, you can find a tab related to triggers. When you click on its name,&amp;nbsp; MySQL will show you the name, the related event, table, timing, and other characteristics regarding each trigger currently in use.&lt;br /&gt;
Let’s introduce you to another interesting fact about MySQL. You already know there are pre-defined system variables, but system functions exist too! System functions can also be called built-in functions. Often applied in practice, they provide data related to the moment of the execution of a certain query.&lt;br /&gt;
&amp;nbsp;For instance, SYSDATE() delivers the date and time of the moment at which you have invoked this function.&lt;br /&gt;
SELECT SYSDATE();&lt;br /&gt;
&amp;nbsp;Another frequently employed function, “Date Format”, assigns a specific format to a given date. For instance, the following query could extract the current date, quoting the year, the month, and the day.&lt;br /&gt;
SELECT DATE_FORMAT(SYSDATE(), &#39;%y-%m-%d&#39;) as today;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;Of course, there are many other ways in which you could format a date; what we showed here was just an example. So, using system functions seems cool, doesn’t it?&lt;br /&gt;
&amp;nbsp;You already know how to work with the syntax that allows you to create triggers.&lt;br /&gt;
As an exercise, try to understand the following query. Technically, it regards the creation of a more complex trigger. It is of the size that professionals often have to deal with.&lt;br /&gt;
&lt;br /&gt;
DELIMITER $$&lt;br /&gt;
CREATE TRIGGER trig_ins_dept_mng&lt;br /&gt;
AFTER INSERT ON dept_manager&lt;br /&gt;
FOR EACH ROW&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;DECLARE v_curr_salary int;&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; SELECT&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;MAX(salary)&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;INTO v_curr_salary FROM&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;salaries&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;WHERE&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;emp_no = NEW.emp_no;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;IF v_curr_salary IS NOT NULL THEN&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;UPDATE salaries&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;SET&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;to_date = SYSDATE()&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;WHERE&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;emp_no = NEW.emp_no and to_date = NEW.to_date;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;INSERT INTO salaries&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;VALUES (NEW.emp_no, v_curr_salary + 20000, NEW.from_date, NEW.to_date);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; END IF;&lt;br /&gt;
END $$&lt;br /&gt;
DELIMITER ;&lt;br /&gt;
&lt;br /&gt;
After you are sure you have understood how this query works, please execute it and then run the following INSERT statement.&lt;br /&gt;
INSERT INTO dept_manager VALUES (&#39;111534&#39;, &#39;d009&#39;, date_format(sysdate(), &#39;%y-%m-%d&#39;), &#39;9999-01-01&#39;);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;SELECT the record of employee number 111534 in the ‘dept_manager’ table, and then in the ‘salaries’ table to see how the output was affected.&lt;br /&gt;
SELECT&lt;br /&gt;
&amp;nbsp; &amp;nbsp; *&lt;br /&gt;
FROM&lt;br /&gt;
&amp;nbsp; &amp;nbsp; dept_manager&lt;br /&gt;
WHERE&lt;br /&gt;
&amp;nbsp; &amp;nbsp; emp_no = 111534;&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
SELECT&lt;br /&gt;
&amp;nbsp; &amp;nbsp; *&lt;br /&gt;
FROM&lt;br /&gt;
&amp;nbsp; &amp;nbsp; salaries&lt;br /&gt;
WHERE&lt;br /&gt;
&amp;nbsp; &amp;nbsp; emp_no = 111534;&lt;br /&gt;
&lt;br /&gt;
Conceptually, this was an ‘after’ trigger that automatically added $20,000 to the salary of the employee who was just promoted as a manager. Moreover, it set the start date of her new contract to be the day on which you executed the insert statement.&lt;br /&gt;
Finally, to restore the data in the database to the state from the beginning of this lecture, execute the following ROLLBACK statement.&lt;br /&gt;
ROLLBACK;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/1616904786129060924'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/1616904786129060924'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2020/01/trigger.html' title='Trigger'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-4123126288451694010</id><published>2020-01-25T08:52:00.000-08:00</published><updated>2020-01-25T09:22:13.805-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>SQL Query</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Please download the script from the &lt;a href=&quot;https://github.com/mahendrakr/sql_script&quot;&gt;GitHub&lt;/a&gt;.&lt;br /&gt;
Schema&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmIklAcoRL4qnk1UBL9I7JHfxCB1FXO_c-bRviWVQ6tSV1GC5SGTW4lD7fp3Ohv_C13rICTKK3gzpDFZAtenSGFbueMZAhpnAULsMMwTwwdF05CuRedulCMi4xCI3anjWDP8RNw13dZV9t/s1600/Joint_table.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;440&quot; data-original-width=&quot;814&quot; height=&quot;344&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmIklAcoRL4qnk1UBL9I7JHfxCB1FXO_c-bRviWVQ6tSV1GC5SGTW4lD7fp3Ohv_C13rICTKK3gzpDFZAtenSGFbueMZAhpnAULsMMwTwwdF05CuRedulCMi4xCI3anjWDP8RNw13dZV9t/s640/Joint_table.PNG&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Database-Schema&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/4123126288451694010'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/4123126288451694010'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2020/01/sql-query.html' title='SQL Query'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmIklAcoRL4qnk1UBL9I7JHfxCB1FXO_c-bRviWVQ6tSV1GC5SGTW4lD7fp3Ohv_C13rICTKK3gzpDFZAtenSGFbueMZAhpnAULsMMwTwwdF05CuRedulCMi4xCI3anjWDP8RNw13dZV9t/s72-c/Joint_table.PNG" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-4884222637389387291</id><published>2020-01-21T23:54:00.000-08:00</published><updated>2020-01-22T00:01:37.276-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>JOINS</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;The SQL tool that allows us to construct a relationship between objects.&lt;/li&gt;
&lt;li&gt;A join shows a result set, containing fields derived from two or more tables.&lt;/li&gt;
&lt;li&gt;We must find a related column from the two tables that contain the same type of data.&lt;/li&gt;
&lt;li&gt;We will be free to add columns from these two tables to our output the columns you use to relate tables must represent the same object, such as id.&lt;/li&gt;
&lt;li&gt;The tables you are considering need not be logically adjacent&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Types of joins&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
INNER JOIN = JOIN&lt;br /&gt;
LEFT JOIN = LEFT OUTER JOIN&lt;br /&gt;
RIGHT JOIN = RIGHT OUTER JOIN&lt;br /&gt;
CROSS JOIN&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;INNER JOIN&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Extract only records in which the values in the related columns match. Null values, or values appearing in just one of the two tables and not appearing in the other, are not displayed.&lt;br /&gt;
Only non-null matching values are in play. And what if such matching values did not exist?&lt;br /&gt;
Simply, the result set will be empty. There will be no link between the two tables.&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Syntax&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
SELECT&lt;br /&gt;
table_1.column_name(s), table_2.column_name(s)&lt;br /&gt;
FROM&lt;br /&gt;
table_1&lt;br /&gt;
JOIN&lt;br /&gt;
&lt;br /&gt;
table_2 ON table_1.column_name = table_2.column_name;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;LEFT JOIN&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
All matching values of the two tables and all values from the left table that match no values from the right table.&lt;br /&gt;
When working with left joins, the order in which you join tables matters.&lt;br /&gt;
Left joins can deliver a list with all records from the left table that do not match any rows from the right table.&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Syntax&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
SELECT&lt;br /&gt;
table_1.column_name(s), table_2.column_name(s)&lt;br /&gt;
FROM&lt;br /&gt;
table_1&lt;br /&gt;
LEFT JOIN&lt;br /&gt;
&lt;br /&gt;
table_2 ON table_1.column_name = table_2.column_name;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;RIGHT JOIN&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Their functionality is identical to LEFT JOINs, with the only difference being that the direction of the operation is inverted.&lt;br /&gt;
When applying a RIGHT JOIN, all the records from the right table will be included in the result set&lt;br /&gt;
values from the left table will be included only if their linking column contains a value coinciding, or matching, with a value from the linking column of the right table&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Syntax&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
SELECT&lt;br /&gt;
table_1.column_name(s), table_2.column_name(s)&lt;br /&gt;
FROM&lt;br /&gt;
table_1&lt;br /&gt;
RIGHT&amp;nbsp; JOIN&lt;br /&gt;
&lt;br /&gt;
table_2 ON table_1.column_name = table_2.column_name;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Note:&lt;/b&gt; LEFT and RIGHT joins are perfect examples of one to many relationships&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;UNION ALL&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Used to combine a few SELECT statements in a single output you can think of it as a tool that allows you to unify tables. We have to select the same number of columns from each table.&lt;br /&gt;
These columns should have the same name, should be in the same order, and should contain related data types.&lt;br /&gt;
&lt;b&gt;&lt;u&gt;SQL SYNTAX&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
SELECT&lt;br /&gt;
N columns&lt;br /&gt;
FROM&lt;br /&gt;
table_1&lt;br /&gt;
UNION ALL SELECT&lt;br /&gt;
N columns&lt;br /&gt;
FROM&lt;br /&gt;
table_2;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Join more than two tables&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
SELECT&lt;br /&gt;
&amp;nbsp; &amp;nbsp; e.first_name,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; e.last_name,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; e.hire_date,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; m.from_date,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; d.dept_name&lt;br /&gt;
FROM&lt;br /&gt;
&amp;nbsp; &amp;nbsp; employees e&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; JOIN&lt;br /&gt;
&amp;nbsp; &amp;nbsp; dept_manager m ON e.emp_no = m.emp_no&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; JOIN&lt;br /&gt;
&amp;nbsp; &amp;nbsp; departments d ON m.dept_no = d.dept_no;&lt;br /&gt;
&lt;b&gt;Here employees, dept_manager and departments.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;UNION vs UNION ALL&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
When uniting two identically organized tables&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;UNION displays only distinct values in the output whereas UNION ALL retrieves the duplicates as well.&lt;/li&gt;
&lt;li&gt;UNION uses more MySQL resources (computational power and storage space) whereas UNION ALL uses less MySQL resources.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
Looking for better results: use UNION&lt;br /&gt;
Seeking to optimize performance: opt for UNION ALL&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;SUBQUERY&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Queries embedded in a query&lt;/li&gt;
&lt;li&gt;subqueries = inner queries = nested queries = inner select&lt;/li&gt;
&lt;li&gt;They are part of another query, called an outer query(outer select) a subquery should always be placed within parentheses.&lt;/li&gt;
&lt;li&gt;The SQL engine starts by running the inner query then it uses its returned output, which is intermediate, to execute the outer query.&lt;/li&gt;
&lt;li&gt;The subquery may return a single value(a scalar), single row, single column, or an entire table&lt;/li&gt;
&lt;li&gt;you can have a lot more than one subquery in your outer query it is possible to nest inner queries within other inner queries&lt;/li&gt;
&lt;li&gt;In that case, the SQL engine would execute the innermost query first, and then each subsequent query, until it runs the outermost query last&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;Select the second highest salary of the employee&lt;/b&gt;.&lt;/div&gt;
&lt;div&gt;
select e1.* from employee e1 where 1 = (select count(distinct salary) from employee e2 where e2.salary&amp;gt;e1.salary);&lt;br /&gt;
Next Topic: &lt;a href=&quot;https://javabymahendra.blogspot.com/2020/01/stored-routines.html&quot;&gt;Stored Routines&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/4884222637389387291'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/4884222637389387291'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2020/01/joins.html' title='JOINS'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-4137374560688859421</id><published>2020-01-21T23:03:00.005-08:00</published><updated>2020-01-22T00:06:05.703-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>Stored Routines</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Routine in a context other than a computer, a usual, fixed action, or series of actions, repeated periodically.&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Stored routine&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
An SQL statement, or a set of SQL statements, that can be stored on the database server&lt;br /&gt;
Whenever a user needs to run the query in question, they can call, reference, or invoke the routine.&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Types of Store routines&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
1. Stored procedures&lt;br /&gt;
2. functions = user define functions not built-in functions like aggregate function ,date function&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;The MySQL Syntax for Stored Procedures&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
DELIMITER $$&lt;br /&gt;
CREATE or replace PROCEDURE procedure_name()&lt;br /&gt;
BEGIN&lt;br /&gt;
SQL QUERY&lt;br /&gt;
END$$&lt;br /&gt;
DELIMITER&lt;b&gt;;&lt;/b&gt;&amp;nbsp; From this moment on, $$ will not act as a delimiter.&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;How can you call the Stored Procedure?&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
call employees.select_employees();&lt;br /&gt;
call employees.select_employees;&lt;br /&gt;
call select_employees();&lt;br /&gt;
call select_employees;&lt;br /&gt;
&lt;b&gt;Here employees is the database name and select_employees is the stored procedure name.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Stored Procedures with an Input Parameter&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
A stored routine can perform a calculation that transforms an &lt;b&gt;input value&lt;/b&gt; in&amp;nbsp;&lt;b&gt;output value.&lt;/b&gt;&lt;br /&gt;
Stored procedures can take an input value and then use it in the query, or queries, written in the body of the procedure, this value is represented by the IN parameter.&lt;br /&gt;
&lt;b&gt;For example:&lt;/b&gt;&lt;br /&gt;
DELIMITER $$&lt;br /&gt;
CREATE PROCEDURE procedure_name&lt;br /&gt;
(in parameter)&lt;br /&gt;
BEGIN&lt;br /&gt;
SELECT * FROM employees&lt;br /&gt;
LIMIT 1000;&lt;br /&gt;
END$$&lt;br /&gt;
DELIMITER ;&lt;br /&gt;
After that calculation is ready, a result will be returned.&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Stored Procedures with an Output Parameter&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
DELIMITER $$&lt;br /&gt;
CREATE PROCEDURE procedure_name&lt;br /&gt;
(in parameter, out parameter)&lt;br /&gt;
BEGIN&lt;br /&gt;
SELECT * FROM employees&lt;br /&gt;
LIMIT 1000;&lt;br /&gt;
END$$&lt;br /&gt;
DELIMITER &lt;b&gt;;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Note :&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Output Parameter:&lt;/b&gt; It will represent the variable containing the output value of the operation executed by the query of the stored procedure.&lt;br /&gt;
Every time you create a procedure containing both an IN and an OUT parameter, remember that you must use the SELECT INTO structure in the query of this object’s body.&lt;br /&gt;
&lt;b&gt;&lt;u&gt;For Example&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
DELIMITER $$&lt;br /&gt;
CREATE PROCEDURE emp_info(in p_first_name varchar(255), in p_last_name varchar(255), out p_emp_no integer)&lt;br /&gt;
BEGIN&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;SELECT&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;e.emp_no&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;INTO p_emp_no FROM&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;employees e&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;WHERE&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;e.first_name = p_first_name&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;AND e.last_name = p_last_name;&lt;br /&gt;
END$$&lt;br /&gt;
DELIMITER ;&lt;br /&gt;
&lt;u&gt;&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;Variables&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
when you are defining a program, such as a stored procedure, for instance, you can say you are using parameters&lt;br /&gt;
Parameters are a more abstract term&lt;br /&gt;
Once the structure has been solidified, then it will be applied to the database. The input value you insert is typically referred to as the &lt;b&gt;argument&lt;/b&gt;, while the obtained output value is stored in a &lt;b&gt;variable.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;IN-OUT parameters&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
input = output&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;User-Defined Functions in MySQL&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
DELIMITER $$&lt;br /&gt;
CREATE FUNCTION function_name(parameter data_type) RETURNS data_type&lt;br /&gt;
DECLARE variable_name data_type&lt;br /&gt;
BEGIN&lt;br /&gt;
SELECT …&lt;br /&gt;
RETURN variable_name&lt;br /&gt;
END$$&lt;br /&gt;
DELIMITER ;&lt;br /&gt;
&lt;b&gt;Note :&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
Parameters: Here you have no OUT parameters to define between the parentheses after the object’s name, all parameters are IN, and since this is well known, you need not explicitly indicate it with the word, ‘&lt;b&gt;IN&lt;/b&gt;’.&lt;br /&gt;
Although there are no OUT parameters, there is a return value&lt;br /&gt;
It is obtained after running the query contained in the body of the function.&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;How we can call the function in MySQL&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
SELECT function_name(input_value);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Difference between Stored Procedure and User-defined Function&lt;/b&gt;&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;&quot;&gt;
 &lt;tbody&gt;
&lt;tr&gt;
  &lt;td style=&quot;border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 225.4pt;&quot; valign=&quot;top&quot; width=&quot;301&quot;&gt;
  &lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Stored Procedure&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 225.4pt;&quot; valign=&quot;top&quot; width=&quot;301&quot;&gt;
  &lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
User-defined function&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 225.4pt;&quot; valign=&quot;top&quot; width=&quot;301&quot;&gt;
  &lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
It doesn’t return a value.&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 225.4pt;&quot; valign=&quot;top&quot; width=&quot;301&quot;&gt;
  &lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Returns a value.&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 225.4pt;&quot; valign=&quot;top&quot; width=&quot;301&quot;&gt;
  &lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
CALL Procedure;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 225.4pt;&quot; valign=&quot;top&quot; width=&quot;301&quot;&gt;
  &lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
SELECT function;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 225.4pt;&quot; valign=&quot;top&quot; width=&quot;301&quot;&gt;
  &lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
It can have multiple OUT parameters.&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 225.4pt;&quot; valign=&quot;top&quot; width=&quot;301&quot;&gt;
  &lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
It can return a single value only.&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 225.4pt;&quot; valign=&quot;top&quot; width=&quot;301&quot;&gt;
  &lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
You can use SELECT, INSERT, UPDATE, DELETE with stored procedures.&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 225.4pt;&quot; valign=&quot;top&quot; width=&quot;301&quot;&gt;
  &lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
But you can use only select statement with function.&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 225.4pt;&quot; valign=&quot;top&quot; width=&quot;301&quot;&gt;
  &lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
You can’t include the procedure in a SELECT statement.&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 225.4pt;&quot; valign=&quot;top&quot; width=&quot;301&quot;&gt;
  &lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
You can easily include function as a column in a SELECT statement.&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Next Topic: &lt;a href=&quot;https://javabymahendra.blogspot.com/2020/01/views.html&quot;&gt;VIEWS&lt;/a&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/4137374560688859421'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/4137374560688859421'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2020/01/stored-routines.html' title='Stored Routines'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-1757274123339715558</id><published>2020-01-21T22:34:00.002-08:00</published><updated>2020-01-21T22:35:07.013-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>MySQL Index</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;The index of table functions as the index of a book&lt;/li&gt;
&lt;li&gt;Data is taken from a column of the table and is stored in a certain order in a distinct place called an index.&lt;/li&gt;
&lt;li&gt;Your datasets will typically contain 100,000+ or even 1,000,000+ records the larger a database is, the slower the process of finding the record or records you need.&lt;/li&gt;
&lt;li&gt;We can use an index that will increase the speed of searches related to a table&lt;/li&gt;
&lt;li&gt;Apply the index on large datasets don&#39;t apply the index on small datasets otherwise the costs of having an index might be higher than the benefits&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Syntax to create an Index&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
CREATE INDEX index_name&lt;br /&gt;
ON table_name (column_1, column_2, …);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Note:&lt;/b&gt; The parentheses serve us to indicate the column names on which our search will be based.&lt;br /&gt;
These must be fields from your data table you will search frequently.&lt;br /&gt;
&lt;b&gt;For example&lt;/b&gt;&lt;br /&gt;
CREATE INDEX i_hire_date ON employees(hire_date);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Composite Indexes&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Applied to multiple columns, not just a single one carefully pick the columns that would optimize your search.&lt;br /&gt;
Primary and unique keys are MySQL indexes they represent columns on which a person would typically base their search.&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/1757274123339715558'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/1757274123339715558'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2020/01/mysql-index.html' title='MySQL Index'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-4976017825413349334</id><published>2020-01-21T22:16:00.002-08:00</published><updated>2020-01-22T00:04:29.439-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>Views</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&amp;nbsp;A virtual table whose contents are obtained from an existing table or tables called base tables.&lt;/li&gt;
&lt;li&gt;The view itself does not contain any real data; the data is physically stored in the base table.&lt;/li&gt;
&lt;li&gt;The view simply shows the data contained in the base table.&lt;/li&gt;
&lt;li&gt;Acts as a dynamic table because it instantly reflects data and structural changes in the base table.&lt;/li&gt;
&lt;li&gt;Don’t forget they are not real, physical data sets, meaning we cannot insert or update the information that has already been extracted. They should be seen as temporary virtual data tables retrieving information from base tables&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Syntax to create a view in MySQL&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
CREATE OR REPLACE VIEW &lt;b&gt;view_name&lt;/b&gt; AS&lt;br /&gt;
SELECT&lt;br /&gt;
column_1, column_2,… column_n&lt;br /&gt;
FROM&lt;br /&gt;
table_name;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;For example:&amp;nbsp;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
CREATE OR REPLACE VIEW v_dept_emp_latest_date AS&lt;br /&gt;
&amp;nbsp; &amp;nbsp; SELECT&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; emp_no, MAX(from_date) AS from_date, MAX(to_date) AS to_date&lt;br /&gt;
&amp;nbsp; &amp;nbsp; FROM&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dept_emp&lt;br /&gt;
&amp;nbsp; &amp;nbsp; GROUP BY emp_no;&lt;br /&gt;
&lt;br /&gt;
A view acts as a shortcut for writing the same SELECT statement every time a new&lt;br /&gt;
the request has been made&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;How to call&amp;nbsp; the view in MySQL&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
SELECT * FROM employees.dept_emp_latest_date;&lt;br /&gt;
&lt;b&gt;Note:&lt;/b&gt; In the above&lt;b&gt; &lt;/b&gt;query&amp;nbsp;&lt;b&gt;employees&lt;/b&gt; is the &lt;b&gt;database name&lt;/b&gt; and &lt;b&gt;dept_emp_latest_date&lt;/b&gt; is the &lt;b&gt;view name.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Benefits of Views&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Saves a lot of coding time.&lt;br /&gt;
Occupies no extra memory&lt;br /&gt;
&lt;br /&gt;
Next Topic: &lt;a href=&quot;https://javabymahendra.blogspot.com/2020/01/mysql-index.html&quot;&gt;MySQL INDEX&lt;/a&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/4976017825413349334'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/4976017825413349334'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2020/01/views.html' title='Views'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-8474024082734268011</id><published>2020-01-06T06:13:00.005-08:00</published><updated>2020-01-22T00:00:10.671-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>SQL Data Type</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;String Data Type&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-table-layout-alt: fixed; mso-yfti-tbllook: 1184; width: 0px;&quot;&gt;
 &lt;tbody&gt;
&lt;tr&gt;
  &lt;td style=&quot;border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 93.0pt;&quot; valign=&quot;top&quot; width=&quot;124&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
String Data Type&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 55.6pt;&quot; valign=&quot;top&quot; width=&quot;74&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 92.15pt;&quot; valign=&quot;top&quot; width=&quot;123&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Storage/&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
MAX_SIZE(Byte)&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 106.3pt;&quot; valign=&quot;top&quot; width=&quot;142&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Example&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;&quot; valign=&quot;top&quot; width=&quot;170&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Remark&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 93.0pt;&quot; valign=&quot;top&quot; width=&quot;124&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Character&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 55.6pt;&quot; valign=&quot;top&quot; width=&quot;74&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
CHAR&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 92.15pt;&quot; valign=&quot;top&quot; width=&quot;123&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Fixed/255&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 106.3pt;&quot; valign=&quot;top&quot; width=&quot;142&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
CHAR(5)&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;&quot; valign=&quot;top&quot; width=&quot;170&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
50% faster&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 93.0pt;&quot; valign=&quot;top&quot; width=&quot;124&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Variable character&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 55.6pt;&quot; valign=&quot;top&quot; width=&quot;74&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
VARCHAR&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 92.15pt;&quot; valign=&quot;top&quot; width=&quot;123&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Variable/65,535&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 106.3pt;&quot; valign=&quot;top&quot; width=&quot;142&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
VARCHAR(5)&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;&quot; valign=&quot;top&quot; width=&quot;170&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
a lot more responsive to the data value inserted&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 93.0pt;&quot; valign=&quot;top&quot; width=&quot;124&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
ENUM&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
(enumeration)&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 55.6pt;&quot; valign=&quot;top&quot; width=&quot;74&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
ENUM&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 92.15pt;&quot; valign=&quot;top&quot; width=&quot;123&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 106.3pt;&quot; valign=&quot;top&quot; width=&quot;142&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
ENUM(‘M’,’F’)&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.6pt;&quot; valign=&quot;top&quot; width=&quot;170&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
MySQL will show an error if you attempt to insert any value &amp;nbsp;different from &quot;M&quot; or
  &quot;F&quot;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Numeric Data Type&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Integer&lt;br /&gt;
Fixed-point&lt;br /&gt;
Floating-point&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Integer&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Integer data types are &lt;b&gt;‘signed’&lt;/b&gt; by default.&lt;br /&gt;
If you want to use a range containing only positive, ‘unsigned’ values, you would have to specify this in your query.&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-table-layout-alt: fixed; mso-yfti-tbllook: 1184;&quot;&gt;
 &lt;tbody&gt;
&lt;tr&gt;
  &lt;td style=&quot;border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 99.0pt;&quot; valign=&quot;top&quot; width=&quot;132&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Numeric Data Type&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Size(byte)&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.85pt;&quot; valign=&quot;top&quot; width=&quot;198&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MIN_VALUE&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (signed/unsigned)&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 5.0cm;&quot; valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MIN_VALUE&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (signed/unsigned)&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 13.5pt; mso-yfti-irow: 1;&quot;&gt;
  &lt;td rowspan=&quot;2&quot; style=&quot;border-top: none; border: solid windowtext 1.0pt; height: 13.5pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 99.0pt;&quot; valign=&quot;top&quot; width=&quot;132&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
TINYINT&lt;/div&gt;
&lt;/td&gt;
  &lt;td rowspan=&quot;2&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 13.5pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
1&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 13.5pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.85pt;&quot; valign=&quot;top&quot; width=&quot;198&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
-128&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 13.5pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 5.0cm;&quot; valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
127&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 13.5pt; mso-yfti-irow: 2;&quot;&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 13.5pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.85pt;&quot; valign=&quot;top&quot; width=&quot;198&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 13.5pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 5.0cm;&quot; valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
255&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 15.0pt; mso-yfti-irow: 3;&quot;&gt;
  &lt;td rowspan=&quot;2&quot; style=&quot;border-top: none; border: solid windowtext 1.0pt; height: 15.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 99.0pt;&quot; valign=&quot;top&quot; width=&quot;132&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
SMALLINT&lt;/div&gt;
&lt;/td&gt;
  &lt;td rowspan=&quot;2&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
2&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.85pt;&quot; valign=&quot;top&quot; width=&quot;198&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
-32,768&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 5.0cm;&quot; valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
32,767&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 11.25pt; mso-yfti-irow: 4;&quot;&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 11.25pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.85pt;&quot; valign=&quot;top&quot; width=&quot;198&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 11.25pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 5.0cm;&quot; valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
65,535&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 11.25pt; mso-yfti-irow: 5;&quot;&gt;
  &lt;td rowspan=&quot;2&quot; style=&quot;border-top: none; border: solid windowtext 1.0pt; height: 11.25pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 99.0pt;&quot; valign=&quot;top&quot; width=&quot;132&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
MEDIUMINT&lt;/div&gt;
&lt;/td&gt;
  &lt;td rowspan=&quot;2&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 11.25pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
3&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 11.25pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.85pt;&quot; valign=&quot;top&quot; width=&quot;198&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
-8,388,608&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 11.25pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 5.0cm;&quot; valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
8,388,607&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 15.0pt; mso-yfti-irow: 6;&quot;&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.85pt;&quot; valign=&quot;top&quot; width=&quot;198&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 15.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 5.0cm;&quot; valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
16,777,215&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 14.25pt; mso-yfti-irow: 7;&quot;&gt;
  &lt;td rowspan=&quot;2&quot; style=&quot;border-top: none; border: solid windowtext 1.0pt; height: 14.25pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 99.0pt;&quot; valign=&quot;top&quot; width=&quot;132&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
INT&lt;/div&gt;
&lt;/td&gt;
  &lt;td rowspan=&quot;2&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 14.25pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
4&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 14.25pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.85pt;&quot; valign=&quot;top&quot; width=&quot;198&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
-2,147,483,648&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 14.25pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 5.0cm;&quot; valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
2,147,483,647&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 12.0pt; mso-yfti-irow: 8;&quot;&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 12.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.85pt;&quot; valign=&quot;top&quot; width=&quot;198&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 12.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 5.0cm;&quot; valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
4,294,967,295&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 12.75pt; mso-yfti-irow: 9;&quot;&gt;
  &lt;td rowspan=&quot;2&quot; style=&quot;border-top: none; border: solid windowtext 1.0pt; height: 12.75pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 99.0pt;&quot; valign=&quot;top&quot; width=&quot;132&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
BIGINT&lt;/div&gt;
&lt;/td&gt;
  &lt;td rowspan=&quot;2&quot; style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 12.75pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 2.0cm;&quot; valign=&quot;top&quot; width=&quot;76&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
8&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 12.75pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.85pt;&quot; valign=&quot;top&quot; width=&quot;198&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
-9,223,372,036,854,775,808&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 12.75pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 5.0cm;&quot; valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
9,223,372,036,854,775,807&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;height: 13.5pt; mso-yfti-irow: 10; mso-yfti-lastrow: yes;&quot;&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 13.5pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 148.85pt;&quot; valign=&quot;top&quot; width=&quot;198&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
0&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; height: 13.5pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 5.0cm;&quot; valign=&quot;top&quot; width=&quot;189&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
18,446,744,073,709,551,615&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Fixed-point&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
DECIMAL&lt;br /&gt;
NUMERIC&lt;br /&gt;
DECIMAL(P, S)&lt;br /&gt;
&lt;b&gt;e.g.&lt;/b&gt; DECIMAL(5,3)&lt;br /&gt;
10.523, here precision is 5 and scale is 3.&lt;br /&gt;
367.875, here precision is 6 and scale is 3&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Floating-point&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;&quot;&gt;
 &lt;tbody&gt;
&lt;tr style=&quot;mso-yfti-firstrow: yes; mso-yfti-irow: 0;&quot;&gt;
  &lt;td style=&quot;border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 134.45pt;&quot; valign=&quot;top&quot; width=&quot;179&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Floating-Point Data Type&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 90.95pt;&quot; valign=&quot;top&quot; width=&quot;121&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Size(byte)&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 112.7pt;&quot; valign=&quot;top&quot; width=&quot;150&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;b&gt;Precision&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 112.7pt;&quot; valign=&quot;top&quot; width=&quot;150&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Max number of digits&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 1;&quot;&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 134.45pt;&quot; valign=&quot;top&quot; width=&quot;179&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
FLOAT&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 90.95pt;&quot; valign=&quot;top&quot; width=&quot;121&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
4&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 112.7pt;&quot; valign=&quot;top&quot; width=&quot;150&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Single&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 112.7pt;&quot; valign=&quot;top&quot; width=&quot;150&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
23&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr style=&quot;mso-yfti-irow: 2; mso-yfti-lastrow: yes;&quot;&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 134.45pt;&quot; valign=&quot;top&quot; width=&quot;179&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
DOUBLE&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 90.95pt;&quot; valign=&quot;top&quot; width=&quot;121&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
8&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 112.7pt;&quot; valign=&quot;top&quot; width=&quot;150&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Double&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 112.7pt;&quot; valign=&quot;top&quot; width=&quot;150&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
53&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;&lt;div&gt;
Next Topic: &lt;a href=&quot;https://javabymahendra.blogspot.com/2020/01/joins.html&quot;&gt;JOINS&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/8474024082734268011'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/8474024082734268011'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2020/01/data-type.html' title='SQL Data Type'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-3692551077234922218</id><published>2020-01-06T05:41:00.000-08:00</published><updated>2020-01-21T23:57:24.272-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="SQL"/><title type='text'>Introduction to SQL</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;b&gt;Structure Query Language:&lt;/b&gt; It is a tool which is used to communicate with the database&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Types of SQL statements&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;1. Data Definition Language(DDL)&lt;/b&gt;&lt;br /&gt;
e.g. CREATE, ALTER, DROP, RENAME, TRUNCATE&lt;br /&gt;
&lt;b&gt;2.&lt;/b&gt; &lt;b&gt;Data Manipulation Language(DML)&lt;/b&gt;&lt;br /&gt;
e.g. SELECT, INSERT, UPDATE, DELETE&lt;br /&gt;
&lt;b&gt;3.&lt;/b&gt; &lt;b&gt;Data Control Language(DCL)&lt;/b&gt;&lt;br /&gt;
e.g. GRANT &amp;amp; REVOKE&lt;br /&gt;
&lt;b&gt;4.&lt;/b&gt; &lt;b&gt;Transaction Control Language(TCL)&lt;/b&gt;&lt;br /&gt;
e.g. COMMIT ROLLBACK&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Data Definition Language(DDL)&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
A set of statements that allow the user to define or modify data structures and objects, such as tables.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The CREATE statement&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
It is used for creating entire databases and database objects like tables.&lt;br /&gt;
&lt;b&gt;How to create a database in MySQL?&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;CREATE DATABASE sales;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;How to create a table in SQL.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;CREATE TABLE &amp;lt;table_name&amp;gt;(&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;lt;col_name&amp;gt; &amp;lt;data_type&amp;gt;&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;lt;col2_name&amp;gt; &amp;lt;data_type&amp;gt;);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;For example:&lt;/b&gt;&lt;br /&gt;
CREATE TABLE sales (&lt;br /&gt;
&amp;nbsp; &amp;nbsp; purchase_number INT,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; customer_id INT,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; item_code VARCHAR(10)&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The ALTER statement&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
It is used when altering existing objects&lt;br /&gt;
&lt;br /&gt;
ALTER TABLE sales&lt;br /&gt;
ADD COLUMN date_of_purchase DATE;&lt;br /&gt;
&lt;br /&gt;
ALTER TABLE sales&lt;br /&gt;
ADD PRIMARY KEY(purchase_number);&lt;br /&gt;
&lt;br /&gt;
ALTER TABLE sales&lt;br /&gt;
ADD UNIQUE KEY (item_code);&lt;br /&gt;
&lt;br /&gt;
ALTER TABLE sales&lt;br /&gt;
DROP INDEX item_code;&lt;br /&gt;
&lt;br /&gt;
ALTER TABLE sales&lt;br /&gt;
CHANGE COLUMN customer_id customer_id INT DEFAULT 0;&lt;br /&gt;
&lt;br /&gt;
ALTER TABLE sales&lt;br /&gt;
CHANGE customer_id&amp;nbsp; cust_id INT;&lt;br /&gt;
&lt;br /&gt;
ALTER TABLE sales&lt;br /&gt;
MODIFY cust_id INT NOT NULL;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The DROP statement&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
It is used for deleting a database object.&lt;br /&gt;
&lt;b&gt;DROP TABLE sales;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The RENAME statement&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
It allows you to rename an object.&lt;br /&gt;
&lt;b&gt;RENAME TABLE sales TO sale;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The TRUNCATE statement&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Instead of deleting an entire table through DROP, we can also remove its data and continue to have the table as an object in the database.&lt;br /&gt;
&lt;b&gt;TRUNCATE TABLE customers;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Data Manipulation Language (DML)&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Its statements allow us to manipulate the data in the tables of a database&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The SELECT statement&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
It is used to retrieve data from database objects, like tables&lt;br /&gt;
&lt;b&gt;SELECT * FROM sales;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The INSERT statement&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
It is used to insert data into tables&lt;br /&gt;
INSERT INTO… VALUES…;&lt;br /&gt;
INSERT INTO sales (purchase_number, date_of_purchase) VALUES(1, ‘2017-10-11’);&lt;br /&gt;
INSERT INTO sales VALUES(1, ‘2017-10-11’);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The UPDATE statement&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
It allows you to renew existing data from your tables.&lt;br /&gt;
&lt;br /&gt;
UPDATE sales&lt;br /&gt;
SET date_of_purchase = &#39;2017-12-12&#39;&lt;br /&gt;
WHERE purchase_number = 1;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;The DELETE statement&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
With DELETE, you can specify precisely what you would like to be removed functions similarly to the TRUNCATE statement&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;TRUNCATE vs. DELETE&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
TRUNCATE allows us to remove all the records contained in a table &lt;b&gt;whereas&lt;/b&gt;&lt;br /&gt;
with DELETE, you can specify precisely what you would like to be removed.&lt;br /&gt;
&lt;br /&gt;
DELETE FROM sales;&lt;br /&gt;
TRUNCATE TABLE sales;&lt;br /&gt;
&lt;br /&gt;
DELETE FROM sales&lt;br /&gt;
WHERE&lt;br /&gt;
purchase_number = 1;&lt;br /&gt;
&lt;br /&gt;
Next Topic: &lt;a href=&quot;https://javabymahendra.blogspot.com/2020/01/data-type.html&quot;&gt;SQL DATA TYPE&lt;/a&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/3692551077234922218'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/3692551077234922218'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2020/01/sql.html' title='Introduction to SQL'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-7992671306687452053</id><published>2020-01-03T11:00:00.002-08:00</published><updated>2020-01-03T11:00:42.957-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="JDBC"/><title type='text'>JDBC Statements</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Statement&lt;br /&gt;
PreparedStatement&lt;br /&gt;
CallableStatement&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;1.Statement&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Statement is an interface available in java.sql package.&lt;br /&gt;
Subclass of Statement interface is provided by Driver vendor. You can create the Statement object using the following methods of Connection interface.&lt;br /&gt;
&lt;b&gt;public Statement createStatement();&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public Statement createStatement(int,int);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public Statement createStatement(int,int,int);&lt;/b&gt;&lt;br /&gt;
You can call one of the following methods on Statement object to submit the SQL statement to Database.&lt;br /&gt;
&lt;b&gt;public int executeUpdate(String sql);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public ResultSet executeQuery(String sql);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public boolean execute(String sql);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;public int executeUpdate(String sql)&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
When you want to submit INSERT or UPDATE or DELETE SQL statements then use &lt;b&gt;executeUpdate() &lt;/b&gt;method which returns the number of records inserted or updated or deleted.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;public ResultSet executeQuery(String sql)&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
When you want to submit SELECT SQL statements the use executeQuery() method which returns the ResultSet object contains the records returned by the SELECT statement.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;public boolean execute(String sql)&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
When you want to submit INSERT, UPDATE, DELETE, SELECT SQL statements then use execute() method which returns the boolean value.&lt;br /&gt;
If the returned value is true which means that SELECT SQL statement is submitted and the ResultSet object is created. Use the following method of statement to get the ResultSet object&lt;br /&gt;
&lt;b&gt;public ResultSet getResultSet();&lt;/b&gt;&lt;br /&gt;
If the returned value is false which means that INSERT, UPDATE or DELETE SQL statement is submitted and the integer number is available which represents the number of records&lt;br /&gt;
inserted, updated or deleted. Use the following method of Statement to get the integer number available&lt;br /&gt;
&lt;b&gt;public int getUpdateCount();&lt;/b&gt;&lt;br /&gt;
Using the single Statement object, you can submit any type of SQL statement and any number of SQL statements.&lt;br /&gt;
&lt;b&gt;For example:&lt;/b&gt;&lt;br /&gt;
Statement st = con.createStatement();&lt;br /&gt;
String sql1 = &quot;insert...&quot;;&lt;br /&gt;
String sql2 = &quot;update...&quot;;&lt;br /&gt;
String sql3 = &quot;delete...&quot;;&lt;br /&gt;
String sql4 = &quot;select...&quot;;&lt;br /&gt;
boolean b1 = st.execute(sql1);&lt;br /&gt;
int x = st.executeUpdate(sql2);&lt;br /&gt;
int y = st.executeUpdate(sql3);&lt;br /&gt;
ResultSet rs = st.executeQuery(sql4);&lt;br /&gt;
When you submit the SQL statement using Statement object then SQL statement will compiled and executed every time.&lt;br /&gt;
Total time = req.time + compile time + execution time + res.time&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;= 5ms + 5ms + 5ms + 5ms =20ms.&lt;br /&gt;
1 SQL stmt = 20ms.&lt;br /&gt;
100 SQL stmts = 2000ms.&lt;br /&gt;
When you provide dynamic values for the SQL statement then you need to use concatination operator, Formatter or format() of String class etc to format the query.&lt;br /&gt;
int sid =101;&lt;br /&gt;
String name = &quot;Shail&quot;;&lt;br /&gt;
String email= &quot;shail@gmail.com&quot;&lt;br /&gt;
long phone = 1234567890;&lt;br /&gt;
String sql =&quot;insert into student values(&quot; +sid+ &quot;,&quot;+name+&quot;,&quot;+email+&quot;,&quot;+phone+&quot;)&quot;;&lt;br /&gt;
String sql = String.format(&quot;insert into student values(%d,&#39;%s&#39;,&#39;%s&#39;,%d)&quot;,sid,name,email,phone);&lt;br /&gt;
Formatter fmt = new Formatter();&lt;br /&gt;
String sql = fmt.format(&quot;insert into student values(%d,&#39;%s&#39;,&#39;%s&#39;,%d)&quot;,sid,name,email,phone).toString();&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;2.PreparedStatement&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
PreparedStatement is an interface available in java.sql package.&lt;br /&gt;
PreparedStatement is extending the Statement interface.&lt;br /&gt;
Subclass of PreparedStatement interface is provided by Driver vendor.&lt;br /&gt;
You can create the PreparedStatement object using the following methods of Connection interface.&lt;br /&gt;
&lt;b&gt;public PreparedStatement prepareStatement(sql);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public PreparedStatement prepareStatement(sql,int,int);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public PreparedStatement prepareStatement(sql,int,int,int);&lt;/b&gt;&lt;br /&gt;
You can call one of the following methods on PreparedStatement object to submit the SQL statment to Database.&lt;br /&gt;
&lt;b&gt;public int executeUpdate();&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public ResultSet executeQuery();&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public boolean execute();&lt;/b&gt;&lt;br /&gt;
Using the single public PreparedStatement prepareStatement(sql);object, you can submit only one SQL statement.&lt;br /&gt;
&lt;b&gt;For example:&lt;/b&gt;&lt;br /&gt;
String sql = &quot;insert...&quot;;&lt;br /&gt;
&amp;nbsp;PreparedStatement ps = con.prepareStatement(sql);&lt;br /&gt;
int x = ps.executeUpdate();&lt;br /&gt;
When you submit the SQL statement using PreparedStatement object then SQL statement will be compiled only once first time and pre-compiled SQL statement wil be&lt;br /&gt;
executed every time.&lt;br /&gt;
Total time = req.time + compile time + execution time + res.time&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;= 5ms + 5ms + 5ms + 5ms =20ms.&lt;br /&gt;
First time = 20ms&lt;br /&gt;
Second time onwards = 5ms +0ms + 5ms + 5ms = 15ms.&lt;br /&gt;
100 SQL stmts =20ms+99*15ms&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; =20ms + 1485ms.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; =1505ms.&lt;br /&gt;
PreparedStatement gives you the place holder mechanism for providing the data dynamically to the SQL statement. You need to use the &quot;&lt;b&gt;?&lt;/b&gt;&quot; symbol for the placeholder.&lt;br /&gt;
To provide the value for placeholder, you need to invoke the setter method depending on the placeholder data type.&lt;br /&gt;
&lt;b&gt;public void setInt(int paramIndex, int val);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public void setString(int paramIndex, String val);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public void setLong(int paramIndex, long val);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public void setDouble(int paramIndex, double val);&lt;/b&gt; etc&lt;br /&gt;
int sid =101;&lt;br /&gt;
String name = &quot;Shail&quot;;&lt;br /&gt;
String email= &quot;shail@gmail.com&quot;&lt;br /&gt;
long phone = 1234567890;&lt;br /&gt;
String sql = &quot;insert into student values(?,?,?)&quot;;&lt;br /&gt;
ps=con.prepareStatement(sql);&lt;br /&gt;
ps.setInt(1,sid);&lt;br /&gt;
ps.setString(2,name);&lt;br /&gt;
ps.setString(3,email);&lt;br /&gt;
ps.setLong(4,phone);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;3.CallableStatement&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
CallableStatement is an interface available in java.sql package.&lt;br /&gt;
CallableStatement is extending PrepareStatement interface.&lt;br /&gt;
Subclass of CallableStatement interface is provided by the Driver vendor.&lt;br /&gt;
You can create the CallableStatement object using the following methods of Connection interface.&lt;br /&gt;
&lt;b&gt;public CallableStatement prepareCall(sql);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public CallableStatement prepareCall(sql,int,int);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public CallableStatement prepareCall(sql,int,int,int);&lt;/b&gt;&lt;br /&gt;
You can call the following mmethod on CallableStatement object to submit the SQL statement to database.&lt;br /&gt;
&lt;b&gt;public boolean execute();&lt;/b&gt;&lt;br /&gt;
Using the single CallableStatement object, you can submit call to only one procedure.&lt;br /&gt;
&lt;b&gt;For example:&lt;/b&gt;&lt;br /&gt;
String sql &quot;call p1(?,?)&quot;;&lt;br /&gt;
CallableStatement cs = con.prepareCall(sql);&lt;br /&gt;
cs.setInt(1,10);&lt;br /&gt;
cs.setInt(2,20);&lt;br /&gt;
cs.execute();&lt;br /&gt;
When you submit the call to stored procedure using CallableStatement object then pre-compiled stored procedure will be executed directly.&lt;br /&gt;
Total time = req.time + compile time + execution time + res.time&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;= 5ms + 0ms + 4*5ms + 5ms =30ms.&lt;br /&gt;
4 SQL stmts * 25 times.&lt;br /&gt;
&amp;nbsp; =30*25.&lt;br /&gt;
&amp;nbsp; =750.&lt;br /&gt;
CallableStatement gives you the placeholder mechanism for providing the data dynamically to the procedure parameters. You need to use&amp;nbsp; the &quot;&lt;b&gt;?&lt;/b&gt;&quot; symbol for the placeholder.&lt;br /&gt;
To provide the value for placeholder, you need to invoke the setter methods depending on the placeholder date type.&lt;br /&gt;
&lt;b&gt;public void setInt(int paramIndex, int val);.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public void setString(int paramIndex, String val);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public void setLong(int paramIndex, long val);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public void setDouble(int paramIndex, double val);&lt;/b&gt; etc&lt;br /&gt;
&lt;br /&gt;
You need to use the following method to specify the OUT parameter.&lt;br /&gt;
public void registerOutParameter(int parameterIndex,int sqlType);&lt;br /&gt;
sqlType is a constant from java.sql.Types class.&lt;br /&gt;
You need to use the following method to access the result of OUT parameter.&lt;br /&gt;
&lt;b&gt;public int getInt(int paramIndex);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public String getString(int paramIndex);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public long getLong(int paramIndex);&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;public double getDouble(int paramIndex);&lt;/b&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/7992671306687452053'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/7992671306687452053'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2020/01/jdbc-statements.html' title='JDBC Statements'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-3468495708261703762</id><published>2020-01-03T04:35:00.001-08:00</published><updated>2020-01-03T05:05:32.213-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Servlets"/><title type='text'>Parameters</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Servlet Instance Creation&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;By default, the servlet instance will be created when the first client will send the request to the servlet.&lt;/li&gt;
&lt;li&gt;Only one instance will be created for one servlet and will be used to process all the requests by using multiple threads.&lt;/li&gt;
&lt;li&gt;If you want to create the instance while starting the server or container then you can use the following:&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;&lt;u&gt;In web.xml&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&amp;lt;servlet&amp;gt;&lt;br /&gt;
&amp;lt;load-on-startup&amp;gt;X&amp;lt;/load-on-startup&amp;gt;&lt;br /&gt;
&amp;lt;/servlet&amp;gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;In Annotation&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
@WebServlet(loadOnStartup=X)&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;Note: X will be int type value. It must be +ve integer.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;It indicates in which order the servlet instance will be created.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Parameters&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Parameter is name-value pair.&lt;/li&gt;
&lt;li&gt;Parameter name and value are of type String.&lt;/li&gt;
&lt;li&gt;The parameter is read-only i.e web container stores the parameter in the corresponding object and you can read and use that value. You can not modify the parameters.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;There are 3 types of parameters&lt;/u&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;ServletRequest Parameters&lt;/li&gt;
&lt;li&gt;ServletConfig Parameters&lt;/li&gt;
&lt;li&gt;ServletContex Parameters&lt;/li&gt;
&lt;/ol&gt;
&lt;u&gt;&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;ServletRequest Parameters&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
Client submitted data which is coming from web client to web server along with HTTP request are called as request parameters&lt;br /&gt;
Web container collects client submitted data and stores that in HttpServletRequest object as request parameters. As a developer, you can collect that data from the request object as follows.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Case 1:&lt;/b&gt;&lt;br /&gt;
String name = request.getParameter(&quot;name&quot;);&lt;br /&gt;
String[] course = request.getParameterValues(&quot;course&quot;);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Case 2:&lt;/b&gt;&lt;br /&gt;
You can access the parameter names and values as Map&lt;br /&gt;
Map&amp;lt;String,String[]&amp;gt; map = request.getParameterMap();&lt;br /&gt;
Set pnames = map.keySet();&lt;br /&gt;
Iterator it = pnames.iterator();&lt;br /&gt;
while(it.hasNext()){&lt;br /&gt;
String parameterName = (String)it.next();&lt;br /&gt;
Object val = map.get(parameterName);&lt;br /&gt;
String[] values = (String[])val;&lt;br /&gt;
for(String value: values){&lt;br /&gt;
System.out.println(value);&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Case 3:&lt;/b&gt;&lt;br /&gt;
To access only request parameters&lt;br /&gt;
Enumeration&amp;lt;String&amp;gt; ens = request.getParameterNames();&lt;br /&gt;
List&amp;lt;String&amp;gt; list = Collections.list(ens);&lt;br /&gt;
for(String pn : list){&lt;br /&gt;
String pv = request.getParameter(pn);&lt;br /&gt;
System.out.println(pn+&quot; &quot;+pv);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The container is storing multiple values for the same key in the map in the form of a String array.&lt;br /&gt;
Map&amp;lt;String,String[]&amp;gt; map = ...;&lt;br /&gt;
String course[] = new String[2];&lt;br /&gt;
course[0]=&quot;Java&quot;;&lt;br /&gt;
course[1]=&quot;JDBC&quot;;&lt;br /&gt;
map.put(&quot;course&quot;,course);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;ServletConfig Parameters&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;ServletConfig is an interface available in javax.servlet package and web container vendor are responsible to provide the subclass for this interface.&lt;/li&gt;
&lt;li&gt;Every servlet will have its own ServletConfig object and can not be shared.&lt;/li&gt;
&lt;li&gt;When you want to use any data which is common for all the users but specific to a particular servlet that data can be specified as config parameters or init parameters.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;With Servlet 2.x: Specify the config parameters in web.xml&lt;/b&gt;&lt;br /&gt;
&amp;lt;servlet&amp;gt;&lt;br /&gt;
&amp;lt;servlet-name&amp;gt;helloServlet&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;
&amp;lt;servlet-class&amp;gt;com.mak.servlets.HelloServlet&amp;lt;/servlet-class&amp;gt;&lt;br /&gt;
&amp;lt;init-param&amp;gt;&lt;br /&gt;
&amp;lt;param-name&amp;gt;email&amp;lt;/param-name&amp;gt;&lt;br /&gt;
&amp;lt;param-value&amp;gt;mak@gmail.com&amp;lt;/param-value&amp;gt;&lt;br /&gt;
&amp;lt;/init-param&amp;gt;&lt;br /&gt;
&amp;lt;/servlet&amp;gt;&lt;br /&gt;
&lt;b&gt;With servlet 3.x: specify the config parameters in servlet class with annotations&lt;/b&gt;&lt;br /&gt;
@WebServlet(name=&quot;helloServlet&quot;,urlPatterns={&quot;hello.do&quot;},&lt;br /&gt;
initParams={@WebInitParam(name=&quot;email&quot;,value=&quot;mak@gmail&quot;)})&lt;br /&gt;
public class HelloServlet extends HttpServlet{}&lt;br /&gt;
&lt;br /&gt;
Web container collects data from either web.xml or annotation and stores that in ServletConfig object as config parameters. As a developer, you can collect that data from the config object as follows.&lt;br /&gt;
String email = config.getInitParameter(&quot;email&quot;);&lt;br /&gt;
You can override the following method in the servlet class to acess the ServletConfig&lt;br /&gt;
&lt;b&gt;public void init(ServletConfig cfg)&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
You can use the following inheritance method from HttpServlet&lt;br /&gt;
&lt;b&gt;public ServletConfig getServletConfig()&lt;/b&gt;&lt;br /&gt;
Here when HttpServlet class init() implementation will be invoked(from init() method) then the config object will be returned otherwise null will be returned.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Assume that HttpServlet class is implemented as follows&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
public abstract class HttpServlet...{&lt;br /&gt;
private ServletConfig config;&lt;br /&gt;
public void init(ServletConfig config){&lt;br /&gt;
this.config=config;&lt;br /&gt;
}&lt;br /&gt;
public ServletConfig getServletConfig(){}&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Case 1:&lt;/b&gt;&lt;br /&gt;
class AServlet extends HttpServlet{&lt;br /&gt;
protected void service(...){&lt;br /&gt;
ServletConfig cfg = getServletConfig(); // inherited&lt;br /&gt;
//return the config object&lt;br /&gt;
//Since init() method from HttpServlet will be called and config will be initialized.&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Case 2 :&lt;/b&gt;&lt;br /&gt;
class BServlet extends HttpServlet{&lt;br /&gt;
public void init(ServletConfig cfg){&lt;br /&gt;
super.init(cfg);&amp;nbsp; &amp;nbsp; // invoking the HttpServlet init() method&lt;br /&gt;
}&lt;br /&gt;
protected void service(...){&lt;br /&gt;
ServletConfig cfg = getServletConfig();&lt;br /&gt;
//returns the config object&lt;br /&gt;
// Since init() method from your class will be called and you are calling HttpServlet init() so config will be initialized.&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Case 3:&lt;/b&gt;&lt;br /&gt;
class CServlet extends HttpServlet{&lt;br /&gt;
public void init(ServletConfig cfg){&lt;br /&gt;
}&lt;br /&gt;
protected void service(...){&lt;br /&gt;
ServletConfig cfg = getServletConfig(); // inherited&lt;br /&gt;
//returns null value&lt;br /&gt;
// Since init() method from your class will be called and you are not calling HttpServlet init() so config won&#39;t be initialized.&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;ServletContext Parameters&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;ServletContex is an interface available in &lt;b&gt;javax.servlet&lt;/b&gt; package and container vendor is responsible to provide the subclass for this interface.&lt;/li&gt;
&lt;li&gt;One web application will have only one ServletContext object i.e ServletContext object can be shared with all the servlets running in the container.&lt;/li&gt;
&lt;li&gt;When you want to use any data which is common for all the users and common to all the servlets then data can be specified as context parameters in the web.xml as&lt;/li&gt;
&lt;li&gt;follows.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;&lt;u&gt;web.xml&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&amp;lt;context-param&amp;gt;&lt;br /&gt;
&amp;lt;param-name&amp;gt;website&amp;lt;/param-name&amp;gt;&lt;br /&gt;
&amp;lt;param-value&amp;gt;www.google.com&amp;lt;/param-value&amp;gt;&lt;br /&gt;
&amp;lt;/context-param&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Web container collects data from web.xml and stores that in ServletContext object as context parameters. As a developer, you can collect that data from the context object as follows:&lt;br /&gt;
&lt;b&gt;String web = context.getInitParameter(&quot;website&quot;);&lt;/b&gt;&lt;br /&gt;
You can use the following method with ServletConfig or ServletContext object to access the corresponding parameter names.&lt;br /&gt;
Enumeration&amp;lt;String&amp;gt; ens = sc.getInitParameterNames();&lt;br /&gt;
List&amp;lt;String&amp;gt; list = Collections.list(ens);&lt;br /&gt;
for(String pn: list){&lt;br /&gt;
String pv = sc.getInitParameter(pn);&lt;br /&gt;
System.out.printl(pv);&lt;br /&gt;
}&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/3468495708261703762'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/3468495708261703762'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2020/01/parameters.html' title='Parameters'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-2860885343158889026</id><published>2020-01-02T23:39:00.000-08:00</published><updated>2020-01-03T04:36:50.781-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="JDBC"/><title type='text'>Introduction</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
JDBC is a technology which is used to interact with the database from Java Application&lt;br /&gt;
JDBC Technology is a part of Java Standard Edition&lt;br /&gt;
JDBC is a Specification provided by Java vendor and implemented by Java Vendor or DB vendor.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;JDBC Versions&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
JDBC 3.0 is released under J2SE 1.4.2&lt;br /&gt;
No updation under J2SE 5.0&lt;br /&gt;
JDBC 4.0 is released under Java SE 6&lt;br /&gt;
JDBC 4.1 is released under Java SE 7&lt;br /&gt;
JDBC 4.2 is released under Java SE 8&lt;br /&gt;
&lt;br /&gt;
Java Program which is using JDBC API&amp;nbsp; is called as JDBC Program.&lt;br /&gt;
Two packages provided under JDBC API called:&lt;br /&gt;
&lt;b&gt;java.sql&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;javax.sql&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Classes and Interfaces under java.sql package&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
DriverManager&lt;br /&gt;
Types&lt;br /&gt;
Driver&lt;br /&gt;
Connection&lt;br /&gt;
Statement&lt;br /&gt;
PreparedStatement&lt;br /&gt;
CallableStatement&lt;br /&gt;
ResultSet&lt;br /&gt;
ResultSetMetaData&lt;br /&gt;
DatabaseMetaData&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Classes and Interfaces under javax.sql package&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
RowSet&lt;br /&gt;
JdbcRowSet&lt;br /&gt;
CachedRowSet&lt;br /&gt;
DataSource&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Types of JDBC Drivers&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
There are four types of JDBC drivers&lt;br /&gt;
Type I Driver:&amp;nbsp; &amp;nbsp;JDBC ODBC Bridge Driver&lt;br /&gt;
Type II Driver:&amp;nbsp; Partial Java and Partial Native Driver&lt;br /&gt;
Type III Driver: Net Protocol Driver&lt;br /&gt;
Type IV Driver: Pure Java Driver&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Pros and Cons of Types of Drivers&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;&quot;&gt;
 &lt;tbody&gt;
&lt;tr&gt;
  &lt;td style=&quot;border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.25pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.25pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Advantages&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.3pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Disadvantages&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.25pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Type I Driver&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.25pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Type I is very easy to use and maintain.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Type I is suitable for migrating an application to Java without changing
  existing ODBC setup.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
No extra software is required for Type I implementation.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
The performance of Type I is acceptable.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.3pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Type I driver implementation is possible in window OS only because
  ODBC drivers are available only with windows.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
The performance of this driver is not excellent but acceptable.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.25pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Type II Driver&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.25pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Type II is faster than all other drivers.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.3pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
In Type II both client and server machine will have the database
  library.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
When the database is migrated then you will get much maintenance because
  you need to re-install client-side libraries in all the client machines.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.25pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Type III Driver&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.25pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
In Type III, client-side DB libraries are moved to the middleware server
  called the IDS server.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Because of this, client-side maintenance is reduced.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.3pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
You need to purchase extra software called the IDS server.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Because of having a middleware server between your program and database
  server, performance will be reduced&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.25pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Type IV Driver&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.25pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
This driver is best among all the drivers and highly recommendable to
  use.&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 150.3pt;&quot; valign=&quot;top&quot; width=&quot;200&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Negligible&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;b&gt;Note: Type I driver support is removed from Java 8.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Type IV Driver&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;&quot;&gt;
 &lt;tbody&gt;
&lt;tr&gt;
  &lt;td style=&quot;border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 99.0pt;&quot; valign=&quot;top&quot; width=&quot;132&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Name&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.55pt;&quot; valign=&quot;top&quot; width=&quot;170&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Pure Java Driver&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 99.0pt;&quot; valign=&quot;top&quot; width=&quot;132&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Vendor&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.55pt;&quot; valign=&quot;top&quot; width=&quot;170&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Database Vendor&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 99.0pt;&quot; valign=&quot;top&quot; width=&quot;132&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Username&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.55pt;&quot; valign=&quot;top&quot; width=&quot;170&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&amp;lt;Database Username&amp;gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 99.0pt;&quot; valign=&quot;top&quot; width=&quot;132&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Password&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.55pt;&quot; valign=&quot;top&quot; width=&quot;170&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
&amp;lt;Database Password&amp;gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 99.0pt;&quot; valign=&quot;top&quot; width=&quot;132&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Software Required&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 127.55pt;&quot; valign=&quot;top&quot; width=&quot;170&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Database, Java&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;For Oracle&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;&quot;&gt;
 &lt;tbody&gt;
&lt;tr&gt;
  &lt;td style=&quot;border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 84.8pt;&quot; valign=&quot;top&quot; width=&quot;113&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Driver Class&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 8.0cm;&quot; valign=&quot;top&quot; width=&quot;312&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
oracle.jdbc.driver.OracleDriver&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 84.8pt;&quot; valign=&quot;top&quot; width=&quot;113&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
URL&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 8.0cm;&quot; valign=&quot;top&quot; width=&quot;312&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Jdbc:oracle:thin:@&amp;lt;host&amp;gt;:&amp;lt;port&amp;gt;:&amp;lt;serverName&amp;gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
e.g. jdbc:oracle:thin:@localhost:1521:XE&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 84.8pt;&quot; valign=&quot;top&quot; width=&quot;113&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Classpath&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 8.0cm;&quot; valign=&quot;top&quot; width=&quot;312&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
ojdbc14.jar&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
ojdbc6.jar&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;u&gt;For MySQL&lt;/u&gt;&lt;/b&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;&quot;&gt;
 &lt;tbody&gt;
&lt;tr&gt;
  &lt;td style=&quot;border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 84.8pt;&quot; valign=&quot;top&quot; width=&quot;113&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Driver Class&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 8.0cm;&quot; valign=&quot;top&quot; width=&quot;302&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
com.mysql.jdbc.Driver&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 84.8pt;&quot; valign=&quot;top&quot; width=&quot;113&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
URL&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 8.0cm;&quot; valign=&quot;top&quot; width=&quot;302&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
jdbc:mysql://&amp;lt;host&amp;gt;:&amp;lt;port&amp;gt;/&amp;lt;dbName&amp;gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
e.g. jdbc:mysql://localhost:3306/testdb&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 84.8pt;&quot; valign=&quot;top&quot; width=&quot;113&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Classpath&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 8.0cm;&quot; valign=&quot;top&quot; width=&quot;302&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
mysql.jar&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
&lt;b&gt;&lt;u&gt;Architecture&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNGVSvOQqHvwRdnweobPlh1g4CczvZJhDHV31FX05oXx7AQEjU64g-erh_SYsPlF5ZONNP9zawow4d-9NzTKAxQgY8eYkubK9lxPVSCbQAjooKu4wmtyUxeXB87_TgiIzzfodbmHEh_Ffk/s1600/JDBC_fig.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;379&quot; data-original-width=&quot;710&quot; height=&quot;212&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNGVSvOQqHvwRdnweobPlh1g4CczvZJhDHV31FX05oXx7AQEjU64g-erh_SYsPlF5ZONNP9zawow4d-9NzTKAxQgY8eYkubK9lxPVSCbQAjooKu4wmtyUxeXB87_TgiIzzfodbmHEh_Ffk/s400/JDBC_fig.PNG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Type IV Driver&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Steps to Write JDBC Program&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Step 1:&lt;/b&gt; Load the Driver class.&lt;br /&gt;
&lt;b&gt;Step 2&lt;/b&gt;: Establish the Connection between the JDBC program and Database.&lt;br /&gt;
&lt;b&gt;Step 3:&lt;/b&gt; Prepare the SQL statement.&lt;br /&gt;
&lt;b&gt;Step 4:&lt;/b&gt; Create the JDBC statement.&lt;br /&gt;
&lt;b&gt;Step 5:&lt;/b&gt; Submit the SQL statement to Database using JDBC statement.&lt;br /&gt;
&lt;b&gt;Step 6:&lt;/b&gt; Process the result.&lt;br /&gt;
&lt;b&gt;Step 7:&lt;/b&gt; Close all the resources.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Create the below database and table in MySQL&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
create database testdb;&lt;br /&gt;
use testdb;&lt;br /&gt;
create table student(sid int primary key,name varchar(20),state varchar(20));&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;JDBC Program to insert a record into the database(MySQL)&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;import java.sql.Connection;&lt;/li&gt;
&lt;li&gt;import java.sql.DriverManager;&lt;/li&gt;
&lt;li&gt;import java.sql.SQLException;&lt;/li&gt;
&lt;li&gt;import java.sql.Statement;&lt;/li&gt;
&lt;li&gt;&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;public class JdbcDemo {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Connection con = null;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Statement st = null;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;try {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;b&gt; &lt;/b&gt;&lt;/span&gt;&lt;b&gt;// Step 1:Load the Driver class.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;Class.forName(&quot;com.mysql.jdbc.Driver&quot;);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&lt;b&gt;// Step 2: Establish the connection.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;final String url = &quot;jdbc:mysql://localhost:3306/testdb&quot;;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;con = DriverManager.getConnection(url, &quot;root&quot;, &quot;password&quot;);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;b&gt;// Step 3: Prepare the SQL statement.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;String sql = &quot;insert into student values(101,&#39;Anu&#39;,&#39;M.P&#39;)&quot;;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;&lt;b&gt;// Step 4: Create JDBC statement&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;st = con.createStatement();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;b&gt; &lt;/b&gt;&lt;/span&gt;&lt;b&gt;// Step 5: Submit the SQL statement to Database using JDBC statement.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;int x = st.executeUpdate(sql);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;b&gt;  &lt;/b&gt;&lt;/span&gt;&lt;b&gt;// Step 6: Process the result.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;if (x == 1) {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;System.out.println(&quot;Record Inserted&quot;);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;} else {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;System.out.println(&quot;Record Not Inserted&quot;);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;} catch (ClassNotFoundException | SQLException e) {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;e.printStackTrace();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;} finally {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;b&gt; &lt;/b&gt;&lt;/span&gt;&lt;b&gt;// Step 7: Close all the resources.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;try {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;if (con != null)&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;con.close();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;if (st != null)&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;st.close();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;} catch (SQLException e) {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;System.out.println(&quot;Exception occur while closing the resources&quot;);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/2860885343158889026'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/2860885343158889026'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2020/01/jdbc.html' title='Introduction'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNGVSvOQqHvwRdnweobPlh1g4CczvZJhDHV31FX05oXx7AQEjU64g-erh_SYsPlF5ZONNP9zawow4d-9NzTKAxQgY8eYkubK9lxPVSCbQAjooKu4wmtyUxeXB87_TgiIzzfodbmHEh_Ffk/s72-c/JDBC_fig.PNG" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-2623753503358824869</id><published>2020-01-02T01:03:00.000-08:00</published><updated>2020-01-02T01:24:43.888-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Servlets"/><title type='text'>Servlet</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Servlet is a web technology that is used to develop server-side web components.&lt;br /&gt;
Web components reside in server and perform the following:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Receive the incoming request data&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Process the request&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Prepare the HTML response&lt;br /&gt;
Servlets will be used to develop Dynamic Web application&lt;br /&gt;
One web application can have one or more web components like Servlets and JSPs.&lt;br /&gt;
One web container can have one or more web applications&lt;br /&gt;
One web server will have only one web container.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkhyblHrfTUDfkedCSzSMa6vyPs2rsfWOxr_XeDpA1MuKGvgKrFhgiO8CQZqSKht2yMX_gah-iiydN151sJiWqyxdQjJ0xdcEiGtzJ85oL7sXnZEXNe6GXzlGZqO00sudzHsHGaRWM8DlQ/s1600/WebServlt_Fig.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;401&quot; data-original-width=&quot;910&quot; height=&quot;175&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkhyblHrfTUDfkedCSzSMa6vyPs2rsfWOxr_XeDpA1MuKGvgKrFhgiO8CQZqSKht2yMX_gah-iiydN151sJiWqyxdQjJ0xdcEiGtzJ85oL7sXnZEXNe6GXzlGZqO00sudzHsHGaRWM8DlQ/s400/WebServlt_Fig.PNG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Client-Server&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;Steps to develop and run the first Servlet 4.x based Web App.&lt;/u&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;1.&lt;/b&gt; Consider the login requirement.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTTJY2vHqR4jaw-PRAzw6m63NrJc761vRs62aAxC6up0cS6lDNhlfO1GVeIrjBtRPzG5Qlru360ai-h9tPh5SxIGxsYArT_Kw8X2KSVOrzYUvGWRVHK7E4vceNrX4cArm_MnkdSomd4shB/s1600/Final_servlet_fig.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;404&quot; data-original-width=&quot;752&quot; height=&quot;213&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTTJY2vHqR4jaw-PRAzw6m63NrJc761vRs62aAxC6up0cS6lDNhlfO1GVeIrjBtRPzG5Qlru360ai-h9tPh5SxIGxsYArT_Kw8X2KSVOrzYUvGWRVHK7E4vceNrX4cArm_MnkdSomd4shB/s400/Final_servlet_fig.PNG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Assignment&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;b&gt;2.&amp;nbsp;&lt;/b&gt;Create the Dynamic Web Project in STS/eclipse as follows:&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Select File-&amp;gt;New-&amp;gt;Dynamic Web Project&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Project Name: Lab1&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Click on New Runtime button (for the first time)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Select Apache Tomcat v9.0 and click on Next button&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Browse the Tomcat Home Directory i.e D:\apache-tomcat-9.0.30&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Click on the finish button&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Click on the finish button&lt;br /&gt;
&lt;b&gt;3.&lt;/b&gt; Observe the following Directory structure of Dynamic Web Project in STS.&lt;br /&gt;
&lt;b&gt;4.&lt;/b&gt; Create login.html under the WebContent folder.&lt;br /&gt;
&lt;b&gt;5&lt;/b&gt;. Create a package com.demo.servlets under Java Rsources&amp;gt;src&lt;br /&gt;
&lt;b&gt;6.&amp;nbsp;&lt;/b&gt;Create LoginServlet.java file in package com.demo.servlets.&lt;br /&gt;
&lt;b&gt;7.&amp;nbsp;&lt;/b&gt;Deploy into Tomcat 9 as follows&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Right-click on Lab1&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Select Run As-&amp;gt;Run on Server&lt;br /&gt;
&lt;b&gt;8.&lt;/b&gt; Open the Browser&lt;br /&gt;
&lt;b&gt;9.&amp;nbsp;&lt;/b&gt;Provide the following URL&lt;br /&gt;
&amp;nbsp; &amp;nbsp;http://localhost:8080/Lab1/login.html&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Required Files&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;login.html&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;html&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;head&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;meta charset=&quot;ISO-8859-1&quot;&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;title&amp;gt;Insert title here&amp;lt;/title&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;/head&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;body&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;form action=&quot;login.do&quot;&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;h1&amp;gt;Account Login&amp;lt;/h1&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;p&amp;gt;Username&amp;lt;/p&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;input type=&quot;text&quot; name=&quot;username&quot;/&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;p&amp;gt;Password&amp;lt;/p&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;input type=&quot;password&quot; name=&quot;password&quot;/&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;p&amp;gt;&amp;lt;input type =&quot;submit&quot; value=&quot;Login&quot;/&amp;gt;&amp;lt;/p&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;/form&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;/body&amp;gt;&lt;/li&gt;
&lt;li&gt;&amp;lt;/html&amp;gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;&lt;u&gt;LoginServlet.java&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;import java.io.IOException;&lt;/li&gt;
&lt;li&gt;import java.io.PrintWriter;&lt;/li&gt;
&lt;li&gt;import javax.servlet.ServletException;&lt;/li&gt;
&lt;li&gt;import javax.servlet.annotation.WebServlet;&lt;/li&gt;
&lt;li&gt;import javax.servlet.http.HttpServlet;&lt;/li&gt;
&lt;li&gt;import javax.servlet.http.HttpServletRequest;&lt;/li&gt;
&lt;li&gt;import javax.servlet.http.HttpServletResponse;&lt;/li&gt;
&lt;li&gt;&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;@WebServlet(&quot;/login.do&quot;)&lt;/li&gt;
&lt;li&gt;public class LoginServlet extends HttpServlet {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;private static final long serialVersionUID = 1L;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;String username=&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;request.getParameter(&quot;username&quot;);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;String password=&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;request.getParameter(&quot;password&quot;);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;System.out.println(username);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;String message=&quot;&quot;;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;if(username.equals(password)) {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;message =&quot;&amp;lt;h1&amp;gt;Welcome&quot;+username+&quot;&amp;lt;h1&amp;gt;&quot;;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}else {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;message=&quot;&amp;lt;h1&amp;gt; Invalid username or password&amp;lt;/h1&amp;gt;&quot;;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;PrintWriter out = response.getWriter();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;out.write(message);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;out.flush();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;out.close();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/2623753503358824869'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/2623753503358824869'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2020/01/servlet.html' title='Servlet'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkhyblHrfTUDfkedCSzSMa6vyPs2rsfWOxr_XeDpA1MuKGvgKrFhgiO8CQZqSKht2yMX_gah-iiydN151sJiWqyxdQjJ0xdcEiGtzJ85oL7sXnZEXNe6GXzlGZqO00sudzHsHGaRWM8DlQ/s72-c/WebServlt_Fig.PNG" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-5051920215995089757</id><published>2019-12-27T03:30:00.000-08:00</published><updated>2020-03-18T12:25:16.391-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><title type='text'>Java 8 Date Time API</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;Why did Java 8 introduce a new date and time API when it already had classes such as Date and Calendar?&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Below are the main reasons&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;1.Bad API design&lt;/b&gt;. For example, The Date class has both date and time components, if you only want time information and not date-related information, you have to set the date-related value to zero. Some aspects of the classes are unintuitive as well. For example, in the Date constructor, the range of date values is 1 to 31 but the range of month values is 0 to 11(not 1 to 12).&lt;br /&gt;
&lt;b&gt;2.&lt;/b&gt;Java.util.Date,java.util.Calendar,java.time.SimpleDateFormatter classes are &lt;b&gt;not thread-safe&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
Java 8 provides very good support for date and time-related functionality in the newly introduced package java.time package.&lt;br /&gt;
Most of the classes in this package are immutable and thread-safe. We will learn LocalDate, LocalTime, LocalDateTime,Instant,Period, Duration,TemporalUnit and DateTimeFormatter.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;The java.time package consists of four sub-packages&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;java.time.temporal:&lt;/b&gt; Access date/time fields and units.&lt;br /&gt;
&lt;b&gt;java.time.format:&lt;/b&gt; Formats the input and output of date/time objects.&lt;br /&gt;
&lt;b&gt;java.time.zone:&lt;/b&gt; Handles time zones.&lt;br /&gt;
&lt;b&gt;java.time.chrono:&lt;/b&gt; Supports calendar systems such as Japanese and Thai Calendars.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;LocalDate&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
java.time.LocalDate represents a date without time or time zone. LocalDate is represented in the ISO-8601 calendar system in a year-month-day format(&lt;b&gt;YYYY-MM-DD&lt;/b&gt;). For example,2019-12-27.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;How to get today&#39;s date?&lt;/b&gt;&lt;br /&gt;
LocalDate today = LocalDate.now();&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;LocalTime&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
java.time.LocalTime class is similar to LocalDate except that LocalTime represents time without dates or time zones. The time is in the ISO-8601 calendar system format &lt;b&gt;HH:MM:SS: nanosecond&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;How to get the current time?&lt;/b&gt;&lt;br /&gt;
LocalTime&amp;nbsp; currentTime = LocalTime.now();&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;LocalDateTime&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
This class &lt;b&gt;java.time.LocalDateTime&lt;/b&gt; represents both data and time without time zones.&lt;br /&gt;
You can think of LocalDateTime as a logical combination of the LocalTime and LocalDate classes. The date and time formats use the ISO-8601 calendar system:&lt;br /&gt;
&lt;b&gt;YYYY-MM-DD HH:MM: SS.nanosecond&lt;/b&gt;. For example,&lt;br /&gt;
LocalDateTime dateTime =LocalDateTime.now();&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Instant&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Represents machine time starting from Unix epoch(&lt;b&gt;1970-01-01T00:00:00Z&lt;/b&gt;)&lt;br /&gt;
Typically used for timestamps&lt;br /&gt;
Instant instant = Instant.now();&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;What is the difference between LocalDateTime and Instant?&lt;/b&gt;&lt;br /&gt;
LocalDateTime uses the default time zone, but Instant does not.&lt;br /&gt;
For examaple,&lt;br /&gt;
LocalDateTime dateTime =LocalDateTime.now();//2019-12-27T15:27:27.139835100&lt;br /&gt;
Instant instant = Instant.now();//2019-12-27T09:57:27.139835100Z&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Period&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Represents the amount of time in terms of years, months and days.&lt;br /&gt;
Typically used for difference between two LocalDate objects.&lt;br /&gt;
&lt;b&gt;How to find out when medicine will expire?&lt;/b&gt;&lt;br /&gt;
&amp;nbsp;LocalDate manufactureDate = LocalDate.of(2018,Month.MARCH,15);&lt;br /&gt;
&amp;nbsp;LocalDate expiryDate = LocalDate.of(2021, Month.JULY, 20);&lt;br /&gt;
&amp;nbsp;Period expire = Period.between(manufactureDate, expiryDate);&lt;br /&gt;
&amp;nbsp;System.out.println(expire);//P3Y4M5D, here P-&amp;gt; Period, Y-&amp;gt; Year, M-&amp;gt; Month, D-&amp;gt; Day&lt;br /&gt;
System.out.println(expire.getYears()+&quot;Years &quot;+expire.getMonths()+&quot;Months &quot;+expire.getDays()+&quot;Days&quot;);//3Years 4Months 5Days&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Duration&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Represents the amount of time in terms of hours, minutes, seconds and fractions of seconds.&lt;br /&gt;
Typically used for difference between two LocalTime objects.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;How to find out how many hours to go, to wish your best friend&#39;s birthday?&lt;/b&gt;&lt;br /&gt;
LocalDateTime comingMidnight = LocalDateTime.of(LocalDate.now().plusDays(1),LocalTime.MIDNIGHT);&lt;br /&gt;
LocalDateTime now1 = LocalDateTime.now();&lt;br /&gt;
Duration duration =Duration.between(now1, comingMidnight);&lt;br /&gt;
System.out.println(duration);//PT8H9M29.7853329S, Here PT-&amp;gt; Period Time, H-&amp;gt; Hour, M-&amp;gt; Minute, S-&amp;gt;Second&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;TemporalUnit&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
The TemporalUnit interface is the part of java.time.temporal package.&lt;br /&gt;
It represents date or time units such as seconds, hours, days, months, years, etc.&lt;br /&gt;
The enumeration java.util.temporal.ChronoUnit implements TemporalUnit interface.&lt;br /&gt;
Instead of using constant values, it is better to use their equivalent enumeration values. Beacuase using enumeration values in ChronoUnit results in more readable code;further, you are less likely to make mistakes. For example,&lt;br /&gt;
System.out.println(Duration.of(1, ChronoUnit.MINUTES).getSeconds());//60&lt;br /&gt;
System.out.println(Duration.of(1, ChronoUnit.HOURS).getSeconds());//3600&lt;br /&gt;
System.out.println(Duration.of(1, ChronoUnit.DAYS).getSeconds());//86400&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Time Zone&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
There are three important classes related to time zones that you need to know in order to work with dates and times across time zones.&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;ZoneId&lt;/li&gt;
&lt;li&gt;ZoneOffset&lt;/li&gt;
&lt;li&gt;ZoneDateTime&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;ZoneId&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
The java.time.ZoneId class represents time zones. Time zones are typically identified using an offset from Greenwich Mean Time(GMT, also known as UTC/Greenwich).&lt;br /&gt;
For example, the time zone of India is Asia/Calcutta&lt;br /&gt;
System.out.println(ZoneId.systemDefault());//Asia/Calcutta&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;ZoneOffset&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
It represents the time-zone offset from UTC/Greenwich. For example, zone ID &quot;Asia/Calcutta&quot; has a zone offset of +5:30(plus 5 hours and 30 minutes) from UTC/Greenwich.&lt;br /&gt;
The ZoneOffset class is the child class of ZoneId class.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;ZonedDateTime&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
If you want all three- date, time, and time zone together. For that, you can use the ZoneDateTime class.&lt;br /&gt;
LocalDate currentDate = LocalDate.now();&lt;br /&gt;
&amp;nbsp;LocalTime currentTime = LocalTime.now();&lt;br /&gt;
&amp;nbsp;ZoneId myZone = ZoneId.systemDefault();&lt;br /&gt;
&amp;nbsp;ZonedDateTime zoneDateTime = ZonedDateTime.of(currentDate,currentTime,myZone);&lt;br /&gt;
System.out.println(zoneDateTime);//2019-12-27T16:14:59.424905800+05:30[Asia/Calcutta]&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Formatting Dates and Times&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Using java.time.format.DateTimeFormatter class you can format the date and time according to your requirement.&lt;br /&gt;
DateTimeFormatter class provides many predefined constants for formatting date and time values.&lt;br /&gt;
Here is a list of a few such predefined formatters.&lt;br /&gt;
ISO_DATE(2019-12-27)&lt;br /&gt;
ISO_TIME(11:30:25.624)&lt;br /&gt;
ISO_ZONED_DATE_TIME(2019-12-27T16:14:59.424905800+05:30[Asia/Calcutta])&lt;br /&gt;
RFC_1123_DATE_TIME(Thu,26 Dec 2019 11:27:30+0530)&lt;br /&gt;
For example,&lt;br /&gt;
&amp;nbsp; LocalTime wakeupTime = LocalTime.of(6,0,0);&lt;br /&gt;
&amp;nbsp; System.out.println(DateTimeFormatter.ISO_TIME.format(wakeupTime));// 06:00:00&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;If you want to use a custom format instead of any of the predefined format.&lt;/b&gt; To do so, you can use the&lt;br /&gt;
&lt;b&gt;ofPattern()&lt;/b&gt; method in the DateTimeFormatter class.&lt;br /&gt;
DateTimeFormatter customFormat = DateTimeFormatter.ofPattern(&quot;dd/MM/yyyy&quot;);&lt;br /&gt;
System.out.println(customFormat.format(LocalDate.of(2019,Month.MARCH,01)));//01/03/2019&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Important letters and their meanings for creating patterns for dates&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;G(era: BC,AD)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;y(year of the era: 2020,20)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Y(week based year: 2020,20)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;M(month:3,Mar,November)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;w(week in year: 12)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;W(week in month: 3)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;E(day name in week: Sun, Sunday)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;D(day of year: )&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;d(day of month)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
class DateFormatter{&lt;br /&gt;
public static void main(String[] args) {&lt;br /&gt;
String[] dateFormats = { &quot;d-M-y&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&quot;dd-MM-yy&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;dd-MMM-yyy&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;dd-MM-yyyy&quot;,&amp;nbsp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&quot;D-MM-yyyy&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&quot;DD-MM-yyyy&quot;,&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;DDD-MM-yyyy&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;d - MMM- Y&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;w&#39;th week of&#39; YY&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;w&#39;th week of&#39; YYY&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&quot;E, dd&#39;th&#39; MMMM,YYYY&quot;,&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;EE, dd&#39;th&#39; MMMM,YYYY&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;EEE, dd&#39;th&#39; MMMM,YYYY&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;EEEE, dd&#39;th&#39; MMMM,YYYY&quot; };&lt;br /&gt;
Arrays.stream(dateFormats)&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;.forEach(i -&amp;gt; System.out.println(DateTimeFormatter.ofPattern(i).format(LocalDate.now())));&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Important letters and their meanings for creating patterns for time&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;a(marker for the text a.m/p.m marker)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;H(hour:value range 0-23)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;k(hour value range 1-24)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;K(hour in an a.m/p.m :value range 0-11)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;h(hour in a.m/p.m : value range 1-12)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;m(minute)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;s(second)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;S(fraction of a second)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;z(time zone: general time-zone format)&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
class TimeFormatter{&lt;br /&gt;
public static void main(String[] args) {&lt;br /&gt;
String[] timeFormats = {&amp;nbsp; &amp;nbsp; &quot;h:mm&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;hh &#39;o&#39;&#39;clock&#39;&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;H:mm a&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;hh:mm:ss:SS&quot;,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;K:mm:ss a&quot;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; };&lt;br /&gt;
Arrays.stream(timeFormats)&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;.forEach(i -&amp;gt; System.out.println(DateTimeFormatter.ofPattern(i).format(LocalTime.now())));&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;br /&gt;
}&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/5051920215995089757'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/5051920215995089757'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2019/12/java-8-date-time-api.html' title='Java 8 Date Time API'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-1034820227946371954</id><published>2019-12-14T07:48:00.000-08:00</published><updated>2019-12-25T07:18:11.130-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Collection"/><title type='text'>Collection</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
The collection framework is very important for fresher and experienced developers. You must have a good hold on it if you want to crack java interviews. You are supposed to prepare theoretical, coding and scenario-based questions related to this topic. I will try to explain each concept and interview questions of the Collection framework in a simple way. Almost, in all the interview it is asked that what is the difference between List, Set, Map, etc. How will you choose which Collection is suitable for a particular situation? With the help of the below diagram, you can answer the above questions with confidence. So please spend some time in the below diagram.&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilXrh4w7YRBa2q9IGWWIR2zcgAr0qr9ISgIREA4Is07rSVhqHrSVZoSlqlsk1gSQ9UNQU8iNVaZJpzjBHGJ_dm33ondo-y2IIr5w-h2BgF6RNincQ06nHleWNnRzJFc4cM2d4TAQTko1Xl/s1600/collection.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1334&quot; data-original-width=&quot;1600&quot; height=&quot;532&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilXrh4w7YRBa2q9IGWWIR2zcgAr0qr9ISgIREA4Is07rSVhqHrSVZoSlqlsk1gSQ9UNQU8iNVaZJpzjBHGJ_dm33ondo-y2IIr5w-h2BgF6RNincQ06nHleWNnRzJFc4cM2d4TAQTko1Xl/s640/collection.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Collection Framework&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/1034820227946371954'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/1034820227946371954'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2019/12/collection.html' title='Collection'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilXrh4w7YRBa2q9IGWWIR2zcgAr0qr9ISgIREA4Is07rSVhqHrSVZoSlqlsk1gSQ9UNQU8iNVaZJpzjBHGJ_dm33ondo-y2IIr5w-h2BgF6RNincQ06nHleWNnRzJFc4cM2d4TAQTko1Xl/s72-c/collection.jpg" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-4940145103095995095</id><published>2019-10-22T10:36:00.001-07:00</published><updated>2019-12-10T23:13:10.662-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="IOC"/><title type='text'>Dependency Injection</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;What is Dependency Injection aka Inversion of control?&lt;/b&gt;&lt;br /&gt;
Let&#39;s understand first &quot;&lt;b&gt;coupling&lt;/b&gt;&quot;. When one class depends on another, they are called coupled.&lt;br /&gt;
Coupled class can&#39;t be tested in isolation.&lt;br /&gt;
&lt;b&gt;For example:&lt;/b&gt; Client depends on&amp;nbsp; BookService, BookService depends on BookDAO, BookDAO depends on JdbcRepository.&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Client{&lt;/li&gt;
&lt;li&gt;public static void main(String[] args){&lt;/li&gt;
&lt;li&gt;Book book = new Book(1,&quot;Effective Java&quot;,&quot;Joshua Bloch&quot;);&lt;/li&gt;
&lt;li&gt;BookService service = new BookService();//dependency&lt;/li&gt;
&lt;li&gt;service.add(book);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Book{&lt;/li&gt;
&lt;li&gt;private int bookId;&lt;/li&gt;
&lt;li&gt;private String name;&lt;/li&gt;
&lt;li&gt;private String author;&lt;/li&gt;
&lt;li&gt;Book(int bookId,String name,String author){&lt;/li&gt;
&lt;li&gt;this.bookId=bookId;&lt;/li&gt;
&lt;li&gt;this.name=name;&lt;/li&gt;
&lt;li&gt;this.author=author;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;//getters &amp;amp; setters&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class BookService{&lt;/li&gt;
&lt;li&gt;private BookDAO bookDao = new BookDAO();//dependency&lt;/li&gt;
&lt;li&gt;public void add(Book book){&lt;/li&gt;
&lt;li&gt;bookDao.save(book);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class BookDAO{&lt;/li&gt;
&lt;li&gt;private JdbcRepository repository = new JdbcRepository();//dependency&lt;/li&gt;
&lt;li&gt;public void save(Book book){&lt;/li&gt;
&lt;li&gt;repository.save(book);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;How can we reduce coupling?&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Program to interfaces(Still have coupling because of new operator).&lt;/li&gt;
&lt;li&gt;Dependency Injection(Pass collaborators in as parameters rather than hard-coding them).&lt;/li&gt;
&lt;li&gt;Centralize Configuration(Capture all the dependencies in one place. Use a &quot;&lt;b&gt;container&lt;/b&gt;&quot; to create and configure your objects.)&lt;/li&gt;
&lt;/ul&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;public class Client {&lt;/li&gt;
&lt;li&gt;public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;BookService service = Container.getBookServiceInstance();&lt;/li&gt;
&lt;li&gt;Book book = new Book(1,&quot;Effective Java&quot;,&quot;Joshua Bloch&quot;);&lt;/li&gt;
&lt;li&gt;service.save(book);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Book{&lt;/li&gt;
&lt;li&gt;private int bookId;&lt;/li&gt;
&lt;li&gt;private String name;&lt;/li&gt;
&lt;li&gt;private String author;&lt;/li&gt;
&lt;li&gt;Book(int bookId,String name,String author){&lt;/li&gt;
&lt;li&gt;this.bookId=bookId;&lt;/li&gt;
&lt;li&gt;this.name=name;&lt;/li&gt;
&lt;li&gt;this.author=author;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;//getters &amp;amp; setters&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;interface BookService{&lt;/li&gt;
&lt;li&gt;public void save(Book book);&lt;/li&gt;
&lt;li&gt;public void delete(int bookId);&lt;/li&gt;
&lt;li&gt;public void update(Book book);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class BookServiceImpl implements BookService{&lt;/li&gt;
&lt;li&gt;&amp;nbsp;private BookDAO bookDao = Container.getBookDaoInstance();&lt;/li&gt;
&lt;li&gt;@Override&lt;/li&gt;
&lt;li&gt;public void save(Book book) {&lt;/li&gt;
&lt;li&gt;bookDao.save(book);&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;@Overridpublic void delete(int bookId) {&lt;/li&gt;
&lt;li&gt;// logic&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;@Override&lt;/li&gt;
&lt;li&gt;public void update(Book book) {&lt;/li&gt;
&lt;li&gt;// logic&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;interface BookDAO{&lt;/li&gt;
&lt;li&gt;public void save(Book book);&lt;/li&gt;
&lt;li&gt;public void delete(int bookId);&lt;/li&gt;
&lt;li&gt;public void update(Book book);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class JdbcImpl implements BookDAO{&lt;/li&gt;
&lt;li&gt;@Override&lt;/li&gt;
&lt;li&gt;public void save(Book book) {&lt;/li&gt;
&lt;li&gt;//logic&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;@Override&lt;/li&gt;
&lt;li&gt;public void delete(int bookId) {&lt;/li&gt;
&lt;li&gt;// logic&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;@Override&lt;/li&gt;
&lt;li&gt;public void update(Book book) {&lt;/li&gt;
&lt;li&gt;// logic&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;}&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class HibernateDao implements BookDAO{&lt;/li&gt;
&lt;li&gt;@Override&lt;/li&gt;
&lt;li&gt;public void save(Book book) {&lt;/li&gt;
&lt;li&gt;System.out.println(&quot;Book is saving into the database&quot;);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;@Override&lt;/li&gt;
&lt;li&gt;public void delete(int bookId) {&lt;/li&gt;
&lt;li&gt;// logic&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;@Override&lt;/li&gt;
&lt;li&gt;public void update(Book book) {&lt;/li&gt;
&lt;li&gt;// logic&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;}&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Container{&lt;/li&gt;
&lt;li&gt;public static BookDAO getBookDaoInstance() {&lt;/li&gt;
&lt;li&gt;return new HibernateDao();&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;public static BookService getBookServiceInstance() {&lt;/li&gt;
&lt;li&gt;return new BookServiceImpl();&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/4940145103095995095'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/4940145103095995095'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2019/10/dependency-injection.html' title='Dependency Injection'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-8742913128223247081</id><published>2019-10-06T12:48:00.001-07:00</published><updated>2019-12-10T23:15:32.497-08:00</updated><title type='text'>Expectations</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 2rem; font-weight: 200; margin: 0px 140px 0px 0px; overflow-wrap: break-word; overflow: hidden; padding: 0px; position: relative; vertical-align: baseline;&quot;&gt;
&lt;label id=&quot;main-doc-header&quot; style=&quot;border: 0px; font-family: inherit; font-size: 28px; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;u&gt;The expectation from Core a Java Developer&lt;/u&gt;&lt;/label&gt;&lt;/h1&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;/h1&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;General&lt;/span&gt;&lt;/u&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
General expectation of client from core java developer is he should strong in Data structure , multithreading and fundamental , client expect when complex question asked from candidate he should attempt and try to solve the problem, the idea of asking complex question is they want to see are developer attempting to solve the problem which he has never solved , if trying to solve what approach taking , how&amp;nbsp; breaking a problem into small&amp;nbsp; part ,how using design principle and pattern.&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
some time client asking an open-ended question, the purpose of asking the open-ended question is, the developer should answer based on his past experience if developer answer in totality with confidence the probability of&amp;nbsp;&amp;nbsp; clearing interview become very high&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
the idea of asking the ambiguous question&amp;nbsp; is the developer should be asked a question to clarify the requirement if the requirement is not clear and then try to solve the problem&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Problem Solving&lt;/span&gt;&lt;/u&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
The idea of asking problem-solving is to evaluate developer design skills, sometimes client not expecting concrete&amp;nbsp;implementation, they want to understand what approach developer taking to solve the design problem, are they giving up or the attempting to solve the problem&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;Fundamental&lt;/u&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
In fundamental client expecting , developer should know purpose of all the keyword , static binding , dynamic binding , overloading rule , overriding rule in terms of access modifier, exception handling, impact of dynamic linking on performance , how to improve performance by using final keyword , whats default implementation of hash code and equal , cloning, immutability, advantage of immutability , importance of final in security , Exception handling rules&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;Data Structure&lt;/u&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
In Data structure the expectation is developer should know all basic data structure and how those basic data structures internally working , base on use case developer should answer question which data structure is the best fit for given use case , how hashing concept is working in hash set and map , how to improve map /set performance by optimizing hash code , whats are time complexity of different operation on data structure , how resizing is happening in data structure , how to use comparable comparator , how to implement different sorting algorithm .&amp;nbsp;&amp;nbsp; How to optimize data structure by changing the capacity and load factor could able to implement one data structure by using another, could able to answer how to implement the data structure&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;Concurrent API&lt;/u&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
developer should know how concurrent hash map internally managing a lock how segmentation is working ,how many thread can work on concurrent hash map , benefit of using concurrent hash map over hash table and synchronize map , what kind of business use case can be implemented by using concurrent hash map , how blocking queue is working what kind of problem can be solved by using blocking queue, when we should use linked blocking queue and when array blocking queue whats implementation of blocking queue, how to use blocking queue in inter-thread communication, whats fail-safe iterator&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
how to implement thread pool, whats advantage of the thread pool, how many types of thread pool do we have, how we can use executor service, how to use executor service to implement parallel /pipeline processing. , what kind of business problem can be solved by a cyclic barrier and count down the latch and how it&#39;s working, how to use semaphore, whats CAS concept (Compare and set), how atomic API is working internally&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Multithreading&lt;/span&gt;&lt;/u&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
the expectation here is developer should know basic of multithreading, should know how to wait, notify, sleep, join is working, how locking is working , whats class label lock , whats object lock, how to implement inter-thread communication by using wait and notify , how volatile is working , how happen before concept is working in terms of volatile , how to implement thread pool in java 4 , how important is immutability in multithreading , whats code can create deadlock, what code can create starvation,&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Serialization&lt;/span&gt;&lt;/u&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
Developer should know the purpose of serialization, purpose of serial version UID, if serial version UID is not defined how JVM generating it, how to customize serialization behavior, how to serialize transient variable how&amp;nbsp;&amp;nbsp; to improve performance by customizing serialization behavior&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;Memory management&lt;/u&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
Developer should know java memory model , should know heap , how garbage collection is working , how to optimize memory , should aware where class metadata storing in memory, should know reason of Perm gen Exception, reason of Out of memory exception, should aware how to do memory profiling, how to identify which code consuming memory&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;Design Pattern&lt;/u&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
Developers should know at least 2 to 3 design patterns thoroughly while explaining use case implementation should use some of the design patterns, must know the best way of implementing singleton pattern, factory pattern, strategy pattern, builder pattern, flyweight pattern, decorator and adapter pattern. Should know at-least 1 example of these patterns implementations from JDK.&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;Design Principle&lt;/u&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
Developer should know SOLID concept very well, whenever explaining solution design principle should reflect in his solution, how important is code for interface concept&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;Object-oriented Concept&lt;/u&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
Developer should know Encapsulation, Polymorphic, Composition, Inheritance, when should use inheritance when should we use composition.&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;Database&lt;/u&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
Developer should able to write some of the queries on join and aggregation, Should aware index, type of index and how indexing is working, should aware all key&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;Spring&lt;/u&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
Knows basics of Spring-like dependency injection (inversion of control), auto wiring (both XML and annotations), bean life cycle, profiling, transaction management and externalization of properties.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;/h1&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Rest&lt;/span&gt;&lt;/u&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
Basic understanding of REST principles (Uniform interface,&amp;nbsp; Stateless interactions, Cacheable, Client-Server, Layered System, Code on Demand. HTTP protocol (HTTP methods, Headers, Error codes) and the concept of resources for REST.&lt;/div&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; style=&quot;background-color: white; border-collapse: collapse; border-spacing: 0px; border: 0px; color: #333333; font-size: 14px; margin: 0px 0px 2px; padding: 0px; vertical-align: baseline; width: 251px;&quot;&gt;&lt;tbody style=&quot;border: 0px; font-family: inherit; font-style: inherit; font-weight: inherit; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; min-height: 8pt; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 style=&quot;background-color: white; border: 0px; font-size: 24px; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;Micro-Services&lt;/u&gt;&lt;/span&gt;&lt;/h1&gt;
&lt;div style=&quot;background-color: white; border: 0px; color: #333333; font-size: 14px; padding: 0px; vertical-align: baseline;&quot;&gt;
What is MicroServices? How it is different from Monolithic architecture? what are the advantages in Microservices w.r.t Monolithic architecture? Small application using Spring boot.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/8742913128223247081'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/8742913128223247081'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2019/10/expectation.html' title='Expectations'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-7017463705259810728</id><published>2019-10-06T12:34:00.002-07:00</published><updated>2020-02-28T18:01:30.081-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java 8"/><title type='text'>Java 8 features</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;&lt;b&gt;Agenda&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;https://javabymahendra.blogspot.com/2019/10/java-8_6.html#fet1&quot;&gt;Lambda Expression&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://javabymahendra.blogspot.com/2019/10/java-8_6.html#fet2&quot;&gt;Functional Interface&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://javabymahendra.blogspot.com/2019/09/default-method.html&quot;&gt;Default Method&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://javabymahendra.blogspot.com/2019/09/in-built-functions.html&quot;&gt;Inbuilt functional interfaces&lt;/a&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1.Consumer&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2.Supplier&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3.Function&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4.Predicate&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://javabymahendra.blogspot.com/2019/09/double-colon-operator.html&quot;&gt;Double colon operator(::)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Stream API&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://javabymahendra.blogspot.com/2019/09/optional.html&quot;&gt;Optional&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Date and Time API&lt;/li&gt;
&lt;/ul&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;fet1&quot;&gt;&lt;u&gt;Lambda Expression a.k.a Anonymous Function.&lt;/u&gt;&lt;/a&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
A function having no&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1. Name&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2. Return type&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3. Modifiers&lt;br /&gt;
Let&#39;s understand the above concept with the help of an example.&lt;br /&gt;
Take a method having name&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;m&lt;/b&gt;&lt;/span&gt;&amp;nbsp;return type&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;int&lt;/b&gt;&lt;/span&gt;&amp;nbsp;and modifier&amp;nbsp;&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;public&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
public&amp;nbsp; int&amp;nbsp; m(){};&lt;br /&gt;
Note here;&amp;nbsp;is optional&lt;br /&gt;
&lt;br /&gt;
Now apply the Lambda Expression to the above method.&lt;br /&gt;
public&amp;nbsp; int&amp;nbsp; m(){};&lt;br /&gt;
means remove&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1. Name i.e m&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2. Return type i.e int&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3.Access Modifiers i.e public&lt;br /&gt;
After removing it becomes&lt;br /&gt;
() {}&lt;br /&gt;
To make it Lambda express we have a link with arrow symbol -&amp;gt; like below.&lt;br /&gt;
&lt;b&gt;()-&amp;gt;{};&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Note here&lt;span style=&quot;color: red;&quot;&gt;;&lt;/span&gt;&amp;nbsp;is mandatory.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;&lt;b&gt;Important points to Remember&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;The method parameter type is optional, Compiler automatically knows the method parameter type which is called&lt;b&gt;&amp;nbsp;Type Inference&lt;/b&gt;. You know Functional Interface contains only one abstract method so the compiler can guess the type of parameter.&lt;/li&gt;
&lt;li&gt;If there is only one parameter then parenthesis () is also optional.&lt;/li&gt;
&lt;li&gt;If there is only one statement inside the method body then curly braces also optional. But if you are removing &lt;b&gt;{}&lt;/b&gt; then also remove the &lt;b&gt;return&lt;/b&gt; keyword otherwise you will get a compilation error.&lt;/li&gt;
&lt;li&gt;If there is no parameter then parenthesis is mandatory.&lt;/li&gt;
&lt;li&gt;If there are more than one parameters then also parenthesis is mandatory.&lt;/li&gt;
&lt;/ol&gt;
Convert below methods in lamda expression&lt;br /&gt;
&lt;b&gt;Example1&lt;/b&gt;&lt;br /&gt;
public int add(int a,int b) {&lt;br /&gt;
return&amp;nbsp; a+b;&lt;br /&gt;
}&lt;br /&gt;
(int a,int b)-&amp;gt;{return a+b};//valid&lt;br /&gt;
(a,b)-&amp;gt;{return a+b};//valid&lt;br /&gt;
(a,b)-&amp;gt;return a+b;//invalid&lt;br /&gt;
(a,b)-&amp;gt;a+b;//valid&lt;br /&gt;
a,b-&amp;gt;{return a+b;};//invalid&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example2&lt;/b&gt;&lt;br /&gt;
public void method(int a){}&lt;br /&gt;
&lt;br /&gt;
(int a)-&amp;gt;{System.out.println(a);};&lt;br /&gt;
(a)-&amp;gt;{System.out.println(a);};&lt;br /&gt;
a-&amp;gt;{System.out.println(a);};&lt;br /&gt;
a-&amp;gt;System.out.println(a);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example3&lt;/b&gt;&lt;br /&gt;
public void methodOne(){}&lt;br /&gt;
&lt;br /&gt;
()-&amp;gt;{System.out.println(&quot;Anu&quot;);};&lt;br /&gt;
()-&amp;gt;System.out.println(&quot;Anu&quot;);&amp;nbsp;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Now think how can you invoke the above lambda express.&lt;br /&gt;
The answer is using the Functional Interface.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;&lt;b&gt;&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;fet2&quot;&gt;Functional Interface.&lt;/a&gt;&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
Functional Interfaces are those which will satisfy the below rules.&lt;br /&gt;
1. Having only one abstract method.&lt;br /&gt;
2. No restriction with the default method&lt;br /&gt;
3. No restriction with the static method.&lt;br /&gt;
&lt;b&gt;For example&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;public interface InterF {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public void m();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;default void m2() {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;default int m3() {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return 0;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static void m4() {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public static void m5() {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;Q. How can we invoke Lambda expression?&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;Ans: Using Functional Interface.&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp;&lt;b&gt;InterF f=()-&amp;gt;{System.out.println(&quot;hello&quot;);};&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp; &amp;nbsp;f.m();&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;public class Test {&lt;/li&gt;
&lt;li&gt;&amp;nbsp; public void m6() {};&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;InterF f=()-&amp;gt;{System.out.println(&quot;Hello Lambda&quot;);};&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; f.m();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
Note: In Java 1.8v&amp;nbsp;&lt;b&gt;@FunctionalInterface&lt;/b&gt;&amp;nbsp;annotation is introduced to specify that the interface is Functional interface and if you not abide functional interface rules then you will get compilation error.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
@FunctionalInterface&lt;br /&gt;
public interface InterF {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void m1();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void m2();&lt;br /&gt;
}&lt;b&gt;//you will get compilation error.&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;/ol&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;&lt;b&gt;Functional Interface w.r.t Inheritance&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
If an interface extends Functional Interface and child interface doesn&#39;t contain any abstract method then the child interface is also a Functional Interface.&lt;br /&gt;
&lt;br /&gt;
@FunctionalInterface&lt;br /&gt;
public interface InterA {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void m1();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
@FunctionalInterface&lt;br /&gt;
public interface InterB extends InterA {&lt;br /&gt;
&amp;nbsp;public void m1();&lt;br /&gt;
}&lt;b&gt;//No compilation error.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
@FunctionalInterface&lt;br /&gt;
public interface InterB extends InterA {&lt;br /&gt;
&amp;nbsp;public void m2();&lt;b&gt;//compilation error&lt;/b&gt;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public interface InterB extends InterA {&lt;br /&gt;
&amp;nbsp;public void m2();//No compilation error because this is not a functional interface. it is a normal interface which can contain any number of abstract methods&lt;br /&gt;
&amp;nbsp;}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Next Topic: &lt;a href=&quot;https://javabymahendra.blogspot.com/2019/09/default-method.html&quot;&gt;Default Method&lt;/a&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Happy coding.&lt;br /&gt;
You can download the code from Github using the below URL.&lt;br /&gt;
&lt;a href=&quot;https://github.com/mahendrakr/java8&quot;&gt;https://github.com/mahendrakr/java8&lt;/a&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/7017463705259810728'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/7017463705259810728'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2019/10/java-8_6.html' title='Java 8 features'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-27833428395703864</id><published>2019-10-05T02:16:00.001-07:00</published><updated>2019-10-12T08:25:13.653-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><title type='text'>Wrapper classes</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Agenda&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Wrapper classes&lt;/li&gt;
&lt;li&gt;The class hierarchy of wrapper class&lt;/li&gt;
&lt;li&gt;Creating an object of the wrapper classes&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Wrapper classes&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;The wrapper classes are used to wrap primitives in an object. So, they can be added to a collection object.&lt;/li&gt;
&lt;li&gt;To define utility functions that are required for the primitives.&lt;/li&gt;
&lt;li&gt;All wrapper classes are immutable.&lt;/li&gt;
&lt;li&gt;Wrapper classes Byte, Short, Integer and Long cache objects with values in the range of -128 to 127. The Character class caches objects with values 0 to 127.&lt;/li&gt;
&lt;li&gt;Wrapper classes Float and Double don&#39;t cache objects for any range of values.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;public class Test{&lt;/li&gt;
&lt;li&gt;public static void main(String[] args){&lt;/li&gt;
&lt;li&gt;Integer a =10;&lt;/li&gt;
&lt;li&gt;Integer b=10;&lt;/li&gt;
&lt;li&gt;System.out.println(a==b);//true&lt;/li&gt;
&lt;li&gt;Integer c=128;&lt;/li&gt;
&lt;li&gt;Integer&amp;nbsp; d =128;&lt;/li&gt;
&lt;li&gt;System.out.println(c==d);//false,reason value 128 is out of range of cache objects .So it will create new object.&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Creating object of the wrapper classes&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;You can create objects of all the wrapper classes in multiple ways&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Assignment :&lt;/b&gt; By assigning a primitive to a wrapper class variable(Auto-boxing)&lt;br /&gt;
Ex: Integer a =10;&lt;br /&gt;
&lt;b&gt;Constructor:&lt;/b&gt; By using wrapper class constructors.&lt;br /&gt;
Ex: Integer a = new Integer(10);&lt;br /&gt;
&lt;b&gt;Static methods:&lt;/b&gt; By calling static method of wrapper classes, like &lt;b&gt;valueOf();&lt;/b&gt;&lt;br /&gt;
Ex:&lt;b&gt;&amp;nbsp;&lt;/b&gt;Integer wrapper = Integer.valueOf(10);&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Wrapper Classes Constructors&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableGrid&quot; style=&quot;border-collapse: collapse; border: none; margin-left: -.25pt; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;&quot;&gt;
 &lt;tbody&gt;
&lt;tr&gt;
  &lt;td style=&quot;border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 78.0pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Wrapper Class&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-left: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 184.25pt;&quot; valign=&quot;top&quot; width=&quot;246&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Constructors&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 78.0pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Byte&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 184.25pt;&quot; valign=&quot;top&quot; width=&quot;246&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Byte(byte a),Byte(String s)&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 78.0pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Short&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 184.25pt;&quot; valign=&quot;top&quot; width=&quot;246&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Short(short a),Short(String s)&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 78.0pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Integer&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 184.25pt;&quot; valign=&quot;top&quot; width=&quot;246&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Integer(int a),Integer(String s)&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 78.0pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Long&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 184.25pt;&quot; valign=&quot;top&quot; width=&quot;246&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Long(long a),Long(String s)&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 78.0pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Float&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 184.25pt;&quot; valign=&quot;top&quot; width=&quot;246&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Float(float a),Float(String s)&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 78.0pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Double&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 184.25pt;&quot; valign=&quot;top&quot; width=&quot;246&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Double(double a),Double(String s)&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 78.0pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Character&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 184.25pt;&quot; valign=&quot;top&quot; width=&quot;246&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Character(char a)&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;border-top: none; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 78.0pt;&quot; valign=&quot;top&quot; width=&quot;104&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Boolean&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;border-bottom: solid windowtext 1.0pt; border-left: none; border-right: solid windowtext 1.0pt; border-top: none; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 184.25pt;&quot; valign=&quot;top&quot; width=&quot;246&quot;&gt;&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;&quot;&gt;
Boolean(boolean a),Boolean(String s)&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;b&gt;Note: Wrapper class Character has no String argument constructor.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;The class hierarchy of wrapper class&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiy5de8UPt7JEI5wSokHS8NhccAdiTn5RuPxvvsXHNNR8GgAmMfYLuis2kp4F1_Aicv0NESWC03BPbHOlbzUZIS8Er7QetPvG7jhtPYXAYw-cMH3pG48dF2kJ9hxP9Az3Dd55KNpz3a4N10/s1600/wrapper_image.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;291&quot; data-original-width=&quot;500&quot; height=&quot;185&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiy5de8UPt7JEI5wSokHS8NhccAdiTn5RuPxvvsXHNNR8GgAmMfYLuis2kp4F1_Aicv0NESWC03BPbHOlbzUZIS8Er7QetPvG7jhtPYXAYw-cMH3pG48dF2kJ9hxP9Az3Dd55KNpz3a4N10/s320/wrapper_image.gif&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;/div&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;u&gt;Conversion of Wrapper Classes to String and Primitive and Vice-Versa&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;String to Wrapper Class:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Integer wrapper= Integer.valueOf(&quot;10&quot;);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;String to Primitive:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&amp;nbsp;int primitive = Integer.parseInt(&quot;10&quot;);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Wrapper to String:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
String String1= String.valueOf(wrapper);&lt;br /&gt;
String String2 = wrapper.toString();&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Wrapper to primitive:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
int intValue = wrapper.intValue();&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Primitive to Wrapper:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Integer wrapper = Integer.valueOf(10);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Primitive to String:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
String string3 = Integer.toString(10);&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Summary of above conversion in Diagram&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkAUs1q35Rz8NDSEow6TNGB7j4Z4Hdt-ZLrjdtlOe_drH3_5qXdBOd3PP0BgjJC5XPJ0K3dh1ldPQYSHnFk0gc7S438FMlCKti34jilGhJIT8Bg6JyOW3L7QaecZELllZz2PODNR0z0zo_/s1600/conversion.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;169&quot; data-original-width=&quot;298&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkAUs1q35Rz8NDSEow6TNGB7j4Z4Hdt-ZLrjdtlOe_drH3_5qXdBOd3PP0BgjJC5XPJ0K3dh1ldPQYSHnFk0gc7S438FMlCKti34jilGhJIT8Bg6JyOW3L7QaecZELllZz2PODNR0z0zo_/s1600/conversion.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/27833428395703864'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/27833428395703864'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2019/10/wrapper-classed.html' title='Wrapper classes'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiy5de8UPt7JEI5wSokHS8NhccAdiTn5RuPxvvsXHNNR8GgAmMfYLuis2kp4F1_Aicv0NESWC03BPbHOlbzUZIS8Er7QetPvG7jhtPYXAYw-cMH3pG48dF2kJ9hxP9Az3Dd55KNpz3a4N10/s72-c/wrapper_image.gif" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-4499020410353569596</id><published>2019-09-22T00:35:00.001-07:00</published><updated>2019-12-10T22:43:15.358-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java 8"/><title type='text'>Optional</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b style=&quot;color: red;&quot;&gt;Q.What is the alternative to null in Java?&lt;/b&gt;&lt;br /&gt;
Java 8 introduces a new class called &lt;b&gt;java.util.Optional&amp;lt;T&amp;gt;&lt;/b&gt; that’s inspired by the ideas of Haskell and Scala. It’s a class that encapsulates an optional value.&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt; &lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;Q.How do you model the absence of a value?&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
Imagine you have the following nested object structure for a person owning a car and having car insurance.&lt;br /&gt;
The Person / Car / Insurance data model&lt;br /&gt;
If you design your classes like below&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Person{&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private Car car;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Car getCar() {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return car;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Car{&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private Insurance insurance;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public Insurance getInsurance() {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return insurance;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Insurance {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private String name;&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; public String getName() {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return name;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Test {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public String getCarInsuranceName(Person person) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return person.getCar().getInsurance().getName();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Test t = new Test();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t.getCarInsuranceName(new Person());&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;&lt;b&gt;Problems with null&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;It’s a source of error. NullPointerException is by far the most common exception in Java.&lt;/li&gt;
&lt;li&gt;It bloats your code. It worsens readability by making it necessary to fill your code with often deeply nested null checks.&lt;/li&gt;
&lt;li&gt;It’s meaningless. It doesn’t have any semantic meaning, and in particular, it represents the wrong way to model the absence of a value in a statically typed language.&lt;/li&gt;
&lt;li&gt;It breaks Java philosophy. Java always hides pointers from developers except in one case: the null pointer.&lt;/li&gt;
&lt;li&gt;It creates a hole in the type system. null carries no type or other information, meaning it can be assigned to any reference type. This is a problem because, when it’s propagated to another part of the system, you have no idea what that null was initially supposed to be.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;I argue that using null to represent the absence of a value is the wrong approach.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;What you need is a better way to model the absence and presence of a value.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Redefining the Person / Car / Insurance data model using Optional&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Person {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private Optional&amp;lt;Car&amp;gt; car;&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; public Optional&amp;lt;Car&amp;gt; getCar() {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return car;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Car {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private Optional&amp;lt;Insurance&amp;gt; insurance;&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; public Optional&amp;lt;Insurance&amp;gt; getInsurance() {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return insurance;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Insurance {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private String name;&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; public String getName() {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; return name;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class OptionalVersion {&lt;/li&gt;
&lt;li&gt;public String getCarInsurance(Optional&amp;lt;Person1&amp;gt; person) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return person.flatMap(Person1::getCar).flatMap(Car::getInsurance).map(Insurance::getName).orElse(&quot;Unknow&quot;);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; OptionalVersion t = new OptionalVersion();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Optional&amp;lt;Person&amp;gt; p = Optional.empty();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(t.getCarInsurance(p));;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;Creating Optional objects&lt;/b&gt;&lt;/span&gt;&lt;/u&gt;&lt;br /&gt;
There are several ways to create&amp;nbsp; Optional objects.&lt;br /&gt;
&lt;b&gt;Empty optional: &lt;/b&gt;&lt;br /&gt;
Optional&amp;lt;Car&amp;gt; optCar = Optional.empty();&lt;br /&gt;
&lt;b&gt;Optional from a non-null value:&lt;/b&gt;&lt;br /&gt;
Optional&amp;lt;Car&amp;gt; optCar = Optional.of(car);&lt;br /&gt;
If car were null, a NullPointerException would be immediately thrown (rather than getting a&lt;br /&gt;
latent error once you try to access properties of the car).&lt;br /&gt;
&lt;b&gt;Optional from null:&lt;/b&gt;&lt;br /&gt;
Optional&amp;lt;Car&amp;gt; optCar = Optional.ofNullable(car);&lt;br /&gt;
If car were null, the resulting Optional object would be empty&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;&lt;b&gt;The methods of the Optional class&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;u&gt;&lt;b&gt;


 
 
 
 


&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;table cellpadding=&quot;4&quot; cellspacing=&quot;0&quot; style=&quot;width: 643px;&quot;&gt;
 &lt;colgroup&gt;&lt;col width=&quot;85&quot;&gt;&lt;/col&gt;
 &lt;col width=&quot;540&quot;&gt;&lt;/col&gt;
 &lt;/colgroup&gt;&lt;tbody&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: 1px solid #000000; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0.1cm;&quot; width=&quot;85&quot;&gt;Method&lt;/td&gt;
  &lt;td style=&quot;border: 1px solid #000000; padding: 0.1cm;&quot; width=&quot;540&quot;&gt;Description&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;85&quot;&gt;empty&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;540&quot;&gt;Returns an empty Optional instance&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;85&quot;&gt;filter&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;540&quot;&gt;If the value is present and matches the given predicate,
   returns this Optional; otherwise&lt;br /&gt;
returns the empty one&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;85&quot;&gt;flatMap&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;540&quot;&gt;If a value is present, returns the Optional resulting from the
   application of the provided&lt;br /&gt;
mapping function to it; otherwise returns the empty Optional&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;85&quot;&gt;get&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;540&quot;&gt;Returns the value wrapped by this Optional if present,
   otherwise throws a NoSuchElementException&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td height=&quot;39&quot; style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;85&quot;&gt;ifPresent&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;540&quot;&gt;If a value is present, invokes the specified consumer with the
   value; otherwise does nothing&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;85&quot;&gt;isPresent&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;540&quot;&gt;Returns true if there is a value present; otherwise false&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;85&quot;&gt;map&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;540&quot;&gt;If a value is present, applies the provided mapping function to
   it&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;85&quot;&gt;of&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;540&quot;&gt;Returns an Optional wrapping the given value or throws a
   NullPointerException if this value&lt;br /&gt;
is null&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;85&quot;&gt;ofNullable&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;540&quot;&gt;Returns an Optional wrapping the given value or the empty
   Optional if this value is null&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;85&quot;&gt;orElse&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;540&quot;&gt;Returns the value if present or the given default value
   otherwise&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;85&quot;&gt;orElseGet&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;540&quot;&gt;Returns the value if present or the one provided by the given
   Supplier otherwise&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;85&quot;&gt;orElseThrow&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;540&quot;&gt;Returns the value if present or throws the exception created by
   the given Supplier otherwise&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;u&gt;&lt;b&gt;&lt;style type=&quot;text/css&quot;&gt;td p { margin-bottom: 0cm; }p { margin-bottom: 0.25cm; line-height: 115%; }&lt;/style&gt; &lt;/b&gt;&lt;/u&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/4499020410353569596'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/4499020410353569596'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2019/09/optional.html' title='Optional'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-3583399284397546699</id><published>2019-09-21T09:42:00.001-07:00</published><updated>2019-12-10T22:42:58.723-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java 8"/><title type='text'>Double colon operator</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Method and Constructor reference by using &quot;::&quot; operator&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;The functional interface method can be mapped to our specified method by using &quot;::&quot; operator. This is called method reference.&lt;/li&gt;
&lt;li&gt;Our specified method can be either a static or instance method.&lt;/li&gt;
&lt;li&gt;The functional interface method and our specified method should have the same argument types except for return-type, method-name, modifiers, etc are not required to match.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Syntax&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
If our specified method is a static method.&lt;br /&gt;
&lt;b&gt;Class-name::method-name&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
If the method is an instance method&lt;br /&gt;
&lt;b&gt;Object-reference::method-name&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Constructor references&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
We also can use &quot;&lt;b&gt;:: &lt;/b&gt;&quot;operator to refer constructors&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;Syntax&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;Class-name::new&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Student{&lt;/li&gt;
&lt;li&gt;private String name;&lt;/li&gt;
&lt;li&gt;Student(String name){&lt;/li&gt;
&lt;li&gt;this.name=name;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;interface A{&lt;/li&gt;
&lt;li&gt;public Student getName(String name);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;class Test{&lt;/li&gt;
&lt;li&gt;public static void main(String[] args){&lt;/li&gt;
&lt;li&gt;A a= s-&amp;gt; new Sample(s);&lt;/li&gt;
&lt;li&gt;a.getName(&quot;Anu&quot;);&lt;/li&gt;
&lt;li&gt;A a1 = Student::new;&lt;/li&gt;
&lt;li&gt;a1.getName(&quot;Kalawati&quot;);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;b&gt;Note: In method and constructor references compulsory the argument types must be matched.&lt;/b&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;&lt;style type=&quot;text/css&quot;&gt;td p { margin-bottom: 0cm; }p { margin-bottom: 0.25cm; line-height: 115%; }&lt;/style&gt; &lt;/b&gt;&lt;/u&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/3583399284397546699'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/3583399284397546699'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2019/09/double-colon-operator.html' title='Double colon operator'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-6467870781264495539</id><published>2019-09-21T06:02:00.000-07:00</published><updated>2019-10-12T08:26:43.966-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java 8"/><title type='text'>Java 8 in built functions</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;&lt;b&gt;IN BUILT FUNCTIONS&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Consumer,Supplier,Function and Predicate all are available under &lt;b&gt;java.util.function&lt;/b&gt; package.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;Comparison&amp;nbsp; of in built functions&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;table cellpadding=&quot;4&quot; cellspacing=&quot;0&quot; style=&quot;width: 643px;&quot;&gt;
 &lt;colgroup&gt;&lt;col width=&quot;104&quot;&gt;&lt;/col&gt;
 &lt;col width=&quot;140&quot;&gt;&lt;/col&gt;
 &lt;col width=&quot;101&quot;&gt;&lt;/col&gt;
 &lt;col width=&quot;136&quot;&gt;&lt;/col&gt;
 &lt;col width=&quot;120&quot;&gt;&lt;/col&gt;
 &lt;/colgroup&gt;&lt;tbody&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: 1px solid #000000; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0.1cm;&quot; width=&quot;104&quot;&gt;Properties&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: 1px solid #000000; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0.1cm;&quot; width=&quot;140&quot;&gt;Consumer&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: 1px solid #000000; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0.1cm;&quot; width=&quot;101&quot;&gt;Supplier&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: 1px solid #000000; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0.1cm;&quot; width=&quot;136&quot;&gt;Function&lt;/td&gt;
  &lt;td style=&quot;border: 1px solid #000000; padding: 0.1cm;&quot; width=&quot;120&quot;&gt;Predicate&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;104&quot;&gt;Purpose&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;140&quot;&gt;To consume some input and perform required operation.It won’t
   return anything&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;101&quot;&gt;To supply some value base on requirement&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;136&quot;&gt;To take some input and perform the required operation and return the
   result.&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;120&quot;&gt;To some input and perform some conditional checks&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;104&quot;&gt;Interface Declaration&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;140&quot;&gt;interface Consumer&amp;lt;T&amp;gt;{}&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;101&quot;&gt;interface Supplier&amp;lt;R&amp;gt;{}&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;136&quot;&gt;interface Function&amp;lt;T,R&amp;gt;{}&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;120&quot;&gt;interface Predicate&amp;lt;T&amp;gt;{}&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;104&quot;&gt;Single abstract method&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;140&quot;&gt;public void accept(T t);&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;101&quot;&gt;public R get();&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;136&quot;&gt;public R apply(T t);&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;120&quot;&gt;public boolean test(T t);&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;104&quot;&gt;Default method&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;140&quot;&gt;andThen()&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;101&quot;&gt;NA&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;136&quot;&gt;andThen(),&lt;br /&gt;
compose()&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;120&quot;&gt;and(),or(),&lt;br /&gt;
negate()&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr valign=&quot;top&quot;&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;104&quot;&gt;Static method&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;140&quot;&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;101&quot;&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm; padding-top: 0cm;&quot; width=&quot;136&quot;&gt;identify()&lt;/td&gt;
  &lt;td style=&quot;border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; border-top: none; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm; padding-top: 0cm;&quot; width=&quot;120&quot;&gt;isEqual()&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;&lt;b&gt;How to call in built functions?&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;b&gt;&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;import java.util.function.Consumer;&lt;/li&gt;
&lt;li&gt;import java.util.function.Function;&lt;/li&gt;
&lt;li&gt;import java.util.function.Predicate;&lt;/li&gt;
&lt;li&gt;import java.util.function.Supplier;&lt;/li&gt;
&lt;li&gt;public class Test {&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //void accept(T t);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Consumer&amp;lt;String&amp;gt; consumer1 = (String s)-&amp;gt;{System.out.println(s);};&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Consumer&amp;lt;String&amp;gt; consumer2=(s)-&amp;gt;{System.out.println(s);};&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Consumer&amp;lt;String&amp;gt; consumer3=s-&amp;gt;System.out.println(s);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; consumer3.accept(&quot;Amit&quot;);&lt;/li&gt;
&lt;li&gt;&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp; T get();&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; Supplier&amp;lt;Integer&amp;gt; supplier1 = ()-&amp;gt;10;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; System.out.println(supplier1.get());&lt;/li&gt;
&lt;li&gt;&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; //R apply(T t);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; Function&amp;lt;String, Integer&amp;gt; function1 = (String s)-&amp;gt;{return 20;};&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; Function&amp;lt;String, Integer&amp;gt; function2 = (s)-&amp;gt;{return 20;};&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; Function&amp;lt;String, Integer&amp;gt; function3 = (s)-&amp;gt; 20;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; Function&amp;lt;String, Integer&amp;gt; function4 = s-&amp;gt;20;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; System.out.println(function4.apply(&quot;Kalawati&quot;));&lt;/li&gt;
&lt;li&gt;&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;//&amp;nbsp; boolean test(T t);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; Predicate&amp;lt;Integer&amp;gt; predicate1=(Integer i)-&amp;gt;{return i%2==0;};&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; Predicate&amp;lt;Integer&amp;gt; predicate2=(i)-&amp;gt;{return i%2==0;};&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; Predicate&amp;lt;Integer&amp;gt; predicate3=(i)-&amp;gt;i%2==0;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; Predicate&amp;lt;Integer&amp;gt; predicate4=i-&amp;gt; i%2==0;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; System.out.println(predicate4.test(20));&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; System.out.println(predicate4.test(15)); &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;&lt;b&gt;Predicate chaining&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
It is possible to combine predicates into a single predicate by using the following methods.&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;and()&lt;/li&gt;
&lt;li&gt;or()&lt;/li&gt;
&lt;li&gt;negate()&lt;/li&gt;
&lt;li&gt;These are exactly same as logical AND, OR, complement operator&lt;/li&gt;
&lt;/ul&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;&lt;b&gt;Function chaining&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
It is also possible to combine functions into a single function by using the following methods&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;andThen()&lt;/li&gt;
&lt;li&gt;compose()&lt;/li&gt;
&lt;/ul&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;&lt;b&gt;Consumer chaining&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
It is also possible to combine consumers into a single consumer by using &lt;b&gt;andThen()&lt;/b&gt; method.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;import java.util.function.Consumer;&lt;/li&gt;
&lt;li&gt;import java.util.function.Function;&lt;/li&gt;
&lt;li&gt;import java.util.function.Predicate;&lt;/li&gt;
&lt;li&gt;public class FunctionChaining {&lt;/li&gt;
&lt;li&gt;&amp;nbsp; &amp;nbsp; public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Predicate&amp;nbsp; chaining&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Predicate&amp;lt;Integer&amp;gt; p1 = i-&amp;gt;i%2==0;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Predicate&amp;lt;Integer&amp;gt; p2 = i-&amp;gt;i%3==0;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(p1.negate().test(10));//false&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(p1.or(p2).test(15));//true&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(p1.or(p2).test(7));//false&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(p1.and(p2).test(12));//true&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(p1.and(p2).test(10));//false&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //Function chaining&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Function&amp;lt;Integer, Integer&amp;gt; f1= i-&amp;gt; i+2;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Function&amp;lt;Integer,Integer&amp;gt; f2= i-&amp;gt;i*3;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(f1.andThen(f2).apply(4));//18&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Note in andThen() f1 will apply first then f2&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; System.out.println(f1.compose(f2).apply(4));//14&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //Note in compose() f2 will apply first and then f2.&lt;/li&gt;
&lt;li&gt;&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; //Consumer chaining&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Consumer&amp;lt;Integer&amp;gt; c1 = i-&amp;gt;System.out.print(i+2);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Consumer&amp;lt;Integer&amp;gt; c2= i-&amp;gt;System.out.print(i*2);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; c1.andThen(c2).accept(4);//68&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;b&gt;&lt;a href=&quot;http://javabymahendra.blogspot.com/2019/09/double-colon-operator.html&quot;&gt;Next Topic : Double Colon Operator&lt;/a&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/6467870781264495539'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/6467870781264495539'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2019/09/in-built-functions.html' title='Java 8 in built functions'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-3812863950955937878</id><published>2019-09-20T23:32:00.001-07:00</published><updated>2020-02-28T17:50:13.901-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java 8"/><title type='text'>Default method</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;&lt;b&gt;Default Methods&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;In Java 1.8v onwards, you can also define the default method inside the interface.&lt;/li&gt;
&lt;li&gt;You can define the default method using the keyword default.&lt;/li&gt;
&lt;li&gt;Default methods are by default available to all implementation classes. Based on our business requirement implementation class can use these default methods directly or override.&lt;/li&gt;
&lt;li&gt;You can&#39;t override object class methods as default methods inside interface otherwise you will get a compilation error. Because you know Object class methods are by default available to each class/interface hence it is not required to override.&lt;/li&gt;
&lt;li&gt;If you try to override Object class methods in the interface, you will get compiler error.&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;Note: You can&#39;t define the default method inside the concrete class and abstract class, otherwise compilation error.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;Let&#39;s understand the above concept through examples.&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;b&gt;Example1:&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;public default void methodOne(){&lt;/li&gt;
&lt;li&gt;System.out.println(&quot;methodOne&quot;);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;public default int methodOne(int a){&lt;/li&gt;
&lt;li&gt;return 10;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;Example2:&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;interface InterA{&lt;/li&gt;
&lt;li&gt;public default int add(int a, int b){&lt;/li&gt;
&lt;li&gt;return a+b;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;class Test implements InterA{&lt;/li&gt;
&lt;li&gt;public static void main(String[] args){&lt;/li&gt;
&lt;li&gt;Test t = new Test();&lt;/li&gt;
&lt;li&gt;t.add(10,20);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;Example3:&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;interface InterA{&lt;/li&gt;
&lt;li&gt;public default void methodOne(){&lt;/li&gt;
&lt;li&gt;System.out.println(&quot;methodOne of InterA&quot;);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;class Test implements InterA{&lt;/li&gt;
&lt;li&gt;public void methodOne(){&lt;/li&gt;
&lt;li&gt;System.out.println(&quot;methodOne of Test&quot;);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;public static void main(String[] args){&lt;/li&gt;
&lt;li&gt;Test t = new Test();&lt;/li&gt;
&lt;li&gt;t.methodOne();//methodOne of Test&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;Example4:&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;interface InterA{&lt;/li&gt;
&lt;li&gt;public default String toString(){&lt;/li&gt;
&lt;li&gt;return null;// compilation error, you can&#39;t override Object class methods.&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;&lt;b&gt;Default method w.r.t Inheritance&lt;/b&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
If two interfaces contain default method with same method signature then you will get ambiguity problem(diamond problem) in implementation class.To overcome this problem compulsory you should override default method in the implementation class otherwise you will get compilation error.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;interface A{&lt;/li&gt;
&lt;li&gt;default void methodOne(){&lt;/li&gt;
&lt;li&gt;System.out.println(&quot;Interface A default method&quot;);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;interface B{&lt;/li&gt;
&lt;li&gt;default void methodOne(){&lt;/li&gt;
&lt;li&gt;System.out.println(&quot;Interface A default method&quot;);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;class Test implements A,B{}//&lt;b&gt;compilation error&lt;/b&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;Q.&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;How to override default method?&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
In the implementation class, you can override the default method by removing keyword &lt;b&gt;default&lt;/b&gt; and abide by overriding rules.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Test implements A,B{&lt;/li&gt;
&lt;li&gt;public void methodOne(){&lt;/li&gt;
&lt;li&gt;System.out.println(&quot;Override default method&quot;);&lt;/li&gt;
&lt;li&gt;//You can also do as below&lt;/li&gt;
&lt;li&gt;&lt;b&gt;A.super.methodOne();&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;B.super.methodOne();&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;public static void main(String args){&lt;/li&gt;
&lt;li&gt;Test t = new Test();&lt;/li&gt;
&lt;li&gt;t.methodOne();&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;b&gt;&lt;a href=&quot;http://javabymahendra.blogspot.com/2019/09/in-built-functions.html&quot;&gt;Next Topic : In Built Functional Interfaces&lt;/a&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/3812863950955937878'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/3812863950955937878'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2019/09/default-method.html' title='Default method'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-7540365749024212488</id><published>2019-09-17T20:51:00.000-07:00</published><updated>2019-12-25T07:18:33.553-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><title type='text'>Generic concept</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Agenda&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;http://javabymahendra.blogspot.com/2019/09/generic.html#gen1&quot;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://javabymahendra.blogspot.com/2019/09/generic.html#gen2&quot;&gt;Generic Classes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://javabymahendra.blogspot.com/2019/09/generic.html#gen3&quot;&gt;Bounded types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://javabymahendra.blogspot.com/2019/09/generic.html#gen4&quot;&gt;Generic methods and wild card character&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://javabymahendra.blogspot.com/2019/09/generic.html#gen5&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;gen1&quot;&gt;Introduction&lt;/a&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
The generic concept is introduced in Java 1.5v&lt;br /&gt;
To resolve two problems of collection.&lt;br /&gt;
&lt;b&gt;Type safety&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;ClassCastException&lt;/b&gt;&lt;br /&gt;
ArrayList list = new ArrayList();//Non generic a.k.a raw type version till java 1.4&lt;br /&gt;
ArrayList&amp;lt;String&amp;gt; list = new ArrayList&amp;lt;String&amp;gt;();//Generic Type&lt;br /&gt;
In Java 1.7 diamond operator &amp;lt;&amp;gt; is introuduce to improve code readability&lt;br /&gt;
ArrayList&amp;lt;String&amp;gt; list = new ArrayList&amp;lt;&amp;gt;();//Generic Type,valid from java 1.7 onwards&lt;br /&gt;
Here you can&#39;t add elements other than String type othewise you will get compilation error.&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
list.add(&quot;Anu&quot;);//valid&lt;br /&gt;
list.add(10);// invalid&lt;br /&gt;
&lt;b&gt;Note1: Polymorphism concept is applicable only for the base type,not for parameter type.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Note2: Parameter type must not be primitive types. It should be any class or interface&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
ArrayList&amp;lt;int&amp;gt; list = new ArrayList&amp;lt;&amp;gt;();// invalid&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;gen2&quot;&gt;Generic Classes&lt;/a&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/u&gt;&lt;/b&gt; Until Java 1.4v ArrayList class is defined as follows.&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
class ArrayList{&lt;br /&gt;
public boolean add(Object o){}&lt;br /&gt;
public Object get(int index){}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
add() method can take Object as the argument and hence we can add any type of the object to the ArrayList. Due to this, we are not getting type of safety.&lt;br /&gt;
The return type of get() method is object hence at the time of retrieval compulsory we should perform typecasting. But in Java 1.5v a generic version of ArrayList class is defined as follows.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvn1M5pXFds7_K2PzBo-ApcNjV5rlVt3tRbdTqxG8_wl3HFpiAe48tRt__PBJ9hVrrZhypHjjHBYD5x6SMo13_oX8vSj6j3ANb3qf5fYj3HEUViaA1jZIe1n0rB1TAKEWNwlAC1p6huLc4/s1600/Generic.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;129&quot; data-original-width=&quot;493&quot; height=&quot;102&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvn1M5pXFds7_K2PzBo-ApcNjV5rlVt3tRbdTqxG8_wl3HFpiAe48tRt__PBJ9hVrrZhypHjjHBYD5x6SMo13_oX8vSj6j3ANb3qf5fYj3HEUViaA1jZIe1n0rB1TAKEWNwlAC1p6huLc4/s400/Generic.PNG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Generic&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;b&gt;Example:&amp;nbsp;&lt;/b&gt;class ArrayList&amp;lt;E&amp;gt;{&lt;br /&gt;
public boolen add(E data){}&lt;br /&gt;
public E get(int index){}&lt;br /&gt;
}&lt;br /&gt;
Based on our requirement E will be replaced with our provided type.&lt;br /&gt;
To hold only String type of objects we can create ArrayList object as follow.&lt;br /&gt;
Example&lt;br /&gt;
ArrayList&amp;lt;String&amp;gt; list = new ArrayList&amp;lt;&amp;gt;():&lt;br /&gt;
For this requirement compiler considered ArrayList class is&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
class ArrayList&amp;lt;String&amp;gt;{&lt;br /&gt;
public boolen add(String data){}&lt;br /&gt;
public String get(int index){}&lt;br /&gt;
}&lt;br /&gt;
&lt;b&gt;add() method can take only String type as argument hence we can add only String type of objects to the List.&lt;/b&gt;&lt;b&gt;Hence through generics, we are getting type safety.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;At the time of retrieval, it is not required to perform any typecasting.&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;How can we create a custom generic class?&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
class Student&amp;lt;T&amp;gt;{}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;gen3&quot;&gt;Bonded Types&lt;/a&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
We can bound the type parameter for a particular range by using the extends keyword. such types are called bounded types.&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
class Test&amp;lt;T&amp;gt;{}&lt;br /&gt;
Test&amp;lt;Integer&amp;gt; t1 = new Test&amp;lt;&amp;gt;();&lt;br /&gt;
Test&amp;lt;String&amp;gt; t2 = new Test&amp;lt;&amp;gt;();&lt;br /&gt;
As the type parameter we can pass any type there is no restriction hence it is unbounded type.&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
class Test&amp;lt;T extends X&amp;gt;{}&lt;br /&gt;
If X is a class then as the type parameter we can pass either X or its child classes.&lt;br /&gt;
If X is an interface then as the type parameter we can pass either X or its implementation classes.&lt;br /&gt;
Example&lt;br /&gt;
class Test&amp;lt;T extends Number&amp;gt;{}&lt;br /&gt;
class Test1{&lt;br /&gt;
public static void main(String[] args){&lt;br /&gt;
Test&amp;lt;Integer&amp;gt; t1 = new Test&amp;lt;&amp;gt;();&lt;br /&gt;
Test&amp;lt;String&amp;gt; t1 = new Test&amp;lt;&amp;gt;();// Compilation Error&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
class Test&amp;lt;T extends Runnable&amp;gt;{}&lt;br /&gt;
class Test1{&lt;br /&gt;
public static void main(String[] args){&lt;br /&gt;
Test&amp;lt;Thread&amp;gt; t1 = new Test&amp;lt;&amp;gt;();// Thread class is implementation class of Runnable interface.&lt;br /&gt;
Test&amp;lt;String&amp;gt; t1 = new Test&amp;lt;&amp;gt;();// Compilation Error&lt;br /&gt;
}&lt;br /&gt;
&lt;b&gt;We can&#39;t define bounded types by using implements and super keyword.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
class Test&amp;lt;T implements Runnable&amp;gt;{}//invalid&lt;br /&gt;
class Test&amp;lt;T super String&amp;gt;{}//invalid&lt;br /&gt;
But implements keyword purpose we can replace with extends keyword.&lt;br /&gt;
As the type parameter, we can use any valid java identifier but it is recommended to use&lt;br /&gt;
&lt;b&gt;T for Type&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;E for Element&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;K for Key&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;V for Value&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
class Test&amp;lt;X&amp;gt;{}//valid&lt;br /&gt;
class Test&amp;lt;Anu&amp;gt;{}//valid&lt;br /&gt;
&lt;b&gt;We can pass any number of type parameters, there is no restriction at all.&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
class Map&amp;lt;K,V&amp;gt;{}&lt;br /&gt;
class Test&amp;lt;A,B,C,D,E....&amp;gt;{}&lt;br /&gt;
We can also define bounded types in combination.&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
class Test&amp;lt;T extends Number &amp;amp; Runnable&amp;gt;{}//valid&lt;br /&gt;
As the type parameter, we can pass any type which extends Number class and implements Runnable interface.&lt;br /&gt;
class Test&amp;lt;T extends Number &amp;amp; String&amp;gt;{}// invalid, we can&#39;t extend more than one class at a time.&lt;br /&gt;
class Test&amp;lt;T extends Runnable &amp;amp; Comparable&amp;gt;{}//valid&lt;br /&gt;
class Test&amp;lt;T extends Runnable &amp;amp; Number&amp;gt;{}//invalid, we have to take 1st class followed by interface.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;gen4&quot;&gt;Generic methods and wild card character(?)&lt;/a&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
public void methodOne(ArrayList&amp;lt;String&amp;gt; list){&lt;br /&gt;
list.add(&quot;Anu&quot;);&lt;br /&gt;
list.add(null);&lt;br /&gt;
list.add(10);// invalid, only string type and null is allowed.}&lt;br /&gt;
&lt;br /&gt;
public void methodOne(ArrayList&amp;lt;?&amp;gt; list){};We can use this method for ArrayList of any type but within method we can&#39;t&lt;br /&gt;
add anything except null.&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
list.add(null);//valid&lt;br /&gt;
list.add(&quot;Anu&quot;);// invalid&lt;br /&gt;
list.add(10);//invalid&lt;br /&gt;
This method is useful whenever we are performing only read operation.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;public void method(ArrayList&amp;lt;? extends X&amp;gt; list):&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;If X is a class then this method is applicable for ArrayList of either X type or its child classes.&lt;/li&gt;
&lt;li&gt;If X is an interface then this method is applicable for ArrayList of either X type or its implementation classes.&lt;/li&gt;
&lt;li&gt;Within the method we can add only &lt;b&gt;null&lt;/b&gt; value.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;public void methodOne(ArrayList&amp;lt;? super X&amp;gt; list):&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;If X is a class then this method is applicable for ArrayList of either X type or its super-types.&lt;/li&gt;
&lt;li&gt;If X is an interface then this method is applicable for ArrayList of either X type or its super-types.&lt;/li&gt;
&lt;li&gt;But within the method, we can add X type objects and null to the List.&lt;/li&gt;
&lt;li&gt;If X is a class then within the method we can add &lt;b&gt;only X type or its sub-types&lt;/b&gt;.&lt;/li&gt;
&lt;li&gt;If X is an interface then within the method we can add &lt;b&gt;only implementation classes of X&lt;/b&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;b&gt;Below example will help you to understand the above concept.&lt;/b&gt;&lt;br /&gt;
&lt;div&gt;
class A{}&lt;/div&gt;
&lt;div&gt;
class B extends A{}&lt;/div&gt;
&lt;div&gt;
class C extends B{}&lt;/div&gt;
&lt;div&gt;
class D extends C{}&lt;/div&gt;
&lt;div&gt;
interface X{}&lt;/div&gt;
&lt;div&gt;
interface Y extends X{}&lt;/div&gt;
&lt;div&gt;
class P&amp;nbsp; implements Q, Y{}&lt;/div&gt;
&lt;div&gt;
interface Q {}&lt;/div&gt;
&lt;div&gt;
class R extends P{}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;import java.util.ArrayList;&lt;/li&gt;
&lt;li&gt;import java.util.List;&lt;/li&gt;
&lt;li&gt;class GenericAtMethod {&lt;/li&gt;
&lt;li&gt;public static&amp;nbsp; void m1(List&amp;lt;? extends B&amp;gt; list) {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;// We can add only null value&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;list.add(null);&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;public static&amp;nbsp; void m2(List&amp;lt;? extends Y&amp;gt; list) {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;//we can add only null value&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;list.add(null);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;public static&amp;nbsp; void m3(List&amp;lt;? super B&amp;gt; list) {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;// we can add only either B type or its sub-types(C,D) or null&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;list.add(null);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;list.add(new C());&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;list.add(new D());&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;public static&amp;nbsp; void m4(List&amp;lt;? super Y&amp;gt; list) {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;//we can add only either implementation classes of Y(P,R) or null&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;list.add(null);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;list.add(new P());&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;list.add(new R());&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;//method m1() will accept only B type or its sub-types(C,D)&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;List&amp;lt;A&amp;gt; l1= new ArrayList&amp;lt;&amp;gt;();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;List&amp;lt;B&amp;gt; l2= new ArrayList&amp;lt;&amp;gt;();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;List&amp;lt;C&amp;gt; l3=new ArrayList&amp;lt;&amp;gt;();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;List&amp;lt;D&amp;gt; l10=new ArrayList&amp;lt;&amp;gt;();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;List&amp;lt;Object&amp;gt; l9=new ArrayList&amp;lt;&amp;gt;();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;//m1(l1); invalid because A is super type of B&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m1(l2);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m1(l3);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m1(l10);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;//method m2() will accept only Y type or its implementation classes(P,R)&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;List&amp;lt;Y&amp;gt; l4 = new ArrayList&amp;lt;&amp;gt;();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;List&amp;lt;P&amp;gt; l5 = new ArrayList&amp;lt;&amp;gt;();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;List&amp;lt;R&amp;gt; l6 = new ArrayList&amp;lt;&amp;gt;();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;List&amp;lt;X&amp;gt; l7 = new ArrayList&amp;lt;&amp;gt;();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;List&amp;lt;Q&amp;gt; l8 = new ArrayList&amp;lt;&amp;gt;();&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m2(l4);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m2(l5);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m2(l6);&lt;/li&gt;
&lt;li&gt;//&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m2(l7) invalid&lt;/li&gt;
&lt;li&gt;//&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m2(l8); invalid&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;//method m3() will accept only List of either B type or its super-types(A,Object)&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m3(l1);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m3(l2);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m3(l9);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;//m3(l3); invalid&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;//m3(l10); invalid&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;//method m4() will accept only List of either Y type or its super types(Y,Object)&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m4(l4);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m4(l7 );&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;m4(l9);&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;Which of the following declarations are valid?&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
ArrayList&amp;lt;String&amp;gt; l1 = new ArrayList&amp;lt;String&amp;gt;();//valid&lt;br /&gt;
ArrayList&amp;lt;String&amp;gt; l2= new ArrayList&amp;lt;&amp;gt;();//valid&lt;br /&gt;
ArrayList&amp;lt;?&amp;gt; l3 = new ArrayList&amp;lt;Integer&amp;gt;();//valid&lt;br /&gt;
ArrayList&amp;lt;?&amp;gt; l4 = new ArrayList&amp;lt;String&amp;gt;();//valid&lt;br /&gt;
ArrayList&amp;lt;? extends Number&amp;gt; l5 = new ArrayList&amp;lt;Integer&amp;gt;();//valid&lt;br /&gt;
ArrayList&amp;lt;? extends Number&amp;gt; l6 = new ArrayList&amp;lt;String&amp;gt;();// invalid&lt;br /&gt;
ArrayList&amp;lt;?&amp;gt; l7 = new ArrayList&amp;lt;? extends Number&amp;gt;();// invalid&lt;br /&gt;
ArrayList&amp;lt;?&amp;gt; l8 = new ArrayList&amp;lt;?&amp;gt;();// invalid&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;We can declare the type parameter either at the class level or method level.&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Declaring type parameter at class level:&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
class Test&amp;lt;T&amp;gt;{&lt;br /&gt;
//we can use T anywhere&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Declaring type parameter at the method level&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
We must declare just before return type.&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;&lt;br /&gt;
public &amp;lt;T&amp;gt; void methodOne(T t){};//valid&lt;br /&gt;
public &amp;lt;T extends Number&amp;gt; void methodOne(T t){};//valid&lt;br /&gt;
public &amp;lt;T extends Number &amp;amp; Runnable&amp;gt; void method(T t){};//valid&lt;br /&gt;
public &amp;lt;T extends Number &amp;amp; Runnable &amp;amp; Comparable&amp;gt; void methodOne(T t){}//valid&lt;br /&gt;
public &amp;lt;T extends Number &amp;amp; Thread&amp;gt; void methodOne(T t){}//invalid&lt;br /&gt;
public &amp;lt;T extends Runnable &amp;amp; Thread&amp;gt; void methodOne(T t){}//invalid&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;a href=&quot;https://www.blogger.com/null&quot; name=&quot;gen5&quot;&gt;Conclusion&lt;/a&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
The generic concept is applicable only at compile time.Hence the following declarations are equal&lt;br /&gt;
ArrayList&amp;lt;String&amp;gt; list = new ArrayList&amp;lt;&amp;gt;();&lt;br /&gt;
ArrayList&amp;lt;Integer&amp;gt; list1 = new ArrayList&amp;lt;I&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
You will get compiler Error, because method &lt;b&gt;methodOne&lt;/b&gt; have same erasure type.&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;public void methodOne(ArrayList&amp;lt;String&amp;gt; l){}&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;public void methodOne(ArrayList&amp;lt;Integer&amp;gt; l){}&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;How compiler works w.r.t Generic code.&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;The compiler first checks type and value is added the same type or not. If not give you Compilation Error.&lt;/li&gt;
&lt;li&gt;If the type and added values are of the same type, then it erasure the type and again validate the source file, if found any violation then give you Compilation Error.&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;u&gt;Limitation of Generic Type&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;1.&lt;/b&gt;You cannot instantiate a generic type using a new operator. For example,&lt;/div&gt;
&lt;div&gt;
T&amp;nbsp; t = new T(); //compiler error&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;2.&lt;/b&gt;You cannot instantiate an array of a generic type. For example,&lt;/div&gt;
&lt;div&gt;
T[] t = new T[3];&amp;nbsp; //compiler error&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;3.&lt;/b&gt;You can declare instance fields of type T, but not of static fields of type T. For example,&lt;/div&gt;
&lt;div&gt;
Class A&amp;lt;T&amp;gt;{&lt;/div&gt;
&lt;div&gt;
T t;&lt;/div&gt;
&lt;div&gt;
static T t1; //compiler error&lt;/div&gt;
&lt;div&gt;
}&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;4.&lt;/b&gt;It is not possible to have generic exception classes. For example,&lt;/div&gt;
&lt;div&gt;
Class GenericException&amp;lt;T&amp;gt; extends Throwable{} // compiler error&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;5.&lt;/b&gt;You cannot instantiate a generic type with primitive types. For example,&lt;/div&gt;
&lt;div&gt;
List&amp;lt;int&amp;gt; list; // compiler error.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/7540365749024212488'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/7540365749024212488'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2019/09/generic.html' title='Generic concept'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgvn1M5pXFds7_K2PzBo-ApcNjV5rlVt3tRbdTqxG8_wl3HFpiAe48tRt__PBJ9hVrrZhypHjjHBYD5x6SMo13_oX8vSj6j3ANb3qf5fYj3HEUViaA1jZIe1n0rB1TAKEWNwlAC1p6huLc4/s72-c/Generic.PNG" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-4242766661380116441.post-8771159526067814039</id><published>2019-09-17T17:47:00.002-07:00</published><updated>2019-12-02T01:14:20.049-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Core Java"/><title type='text'>Exception handling</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Agenda&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Exception&lt;/li&gt;
&lt;li&gt;Exception Handling&lt;/li&gt;
&lt;li&gt;Benefits of Exception Handling&lt;/li&gt;
&lt;li&gt;Hierarchy of Exception&lt;/li&gt;
&lt;li&gt;Keywords related to exception&lt;/li&gt;
&lt;li&gt;Default exception handling in java&lt;/li&gt;
&lt;li&gt;The possible combination of try, catch and finally&lt;/li&gt;
&lt;li&gt;Multi-catch block&lt;/li&gt;
&lt;li&gt;Try-with resources&lt;/li&gt;
&lt;li&gt;Custom Exception&lt;/li&gt;
&lt;li&gt;Assertions&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Exception&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
An event that disturbs the normal flow of the program is called an exception.&lt;br /&gt;
&lt;b&gt;Examples:&lt;/b&gt;&lt;br /&gt;
NullPointerException&lt;br /&gt;
ArrayIndexOutOfBoundException&lt;br /&gt;
FileNotFoundException&lt;br /&gt;
IOException&lt;br /&gt;
If an exception raised at any particular line, then the program will terminate abnormally. This means the remaining lines of the program will not be executed.&lt;br /&gt;
&lt;br /&gt;
Q. How can you stop abnormal termination of the program?&lt;br /&gt;
Ans: Exception Handling mechanism, using keywords try, catch.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Exception Handling&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Put the risky codes inside a try block and handle it in the catch block.&lt;br /&gt;
Risky codes are those which cause the abnormal termination of the program.&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;public class Test {&lt;/li&gt;
&lt;li&gt;public static void print() {&lt;/li&gt;
&lt;li&gt;int a=10;&lt;/li&gt;
&lt;li&gt;int b=0;&lt;/li&gt;
&lt;li&gt;System.out.println(a/b);// This is risky&amp;nbsp; code, cause it will throw ArithmeticException&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;print();&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;b&gt;To solve the above problem put the risky code in a try block and handle it in a catch block like below.&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;class Solution{&lt;/li&gt;
&lt;li&gt;public static void print() {&lt;/li&gt;
&lt;li&gt;int a=10;&lt;/li&gt;
&lt;li&gt;int b=0;&lt;/li&gt;
&lt;li&gt;try {&lt;/li&gt;
&lt;li&gt;System.out.println(a/b);&lt;/li&gt;
&lt;li&gt;}catch(ArithmeticException e) {&lt;/li&gt;
&lt;li&gt;System.out.println(&quot;Denominator must not be zero&quot;);&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;public static void main(String[] args) {&lt;/li&gt;
&lt;li&gt;print();&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;li&gt;}&lt;/li&gt;
&lt;/ol&gt;
&lt;u&gt;&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;Benefits Of Exception Handling&lt;/span&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
Abnormal termination of the program is solved.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Exception:&lt;/b&gt; It is caused by our program and these are recoverable.&lt;br /&gt;
&lt;b&gt;Error:&lt;/b&gt; It is not caused by our program, these are due to lack of system resources and these are not recoverable.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Hierarchy of Exception&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAtx91hEKGq59Vx9IpoEhNms5KaL_fromYTjqMUWHAXXhvFjM2_ARU4t4WcyyCNT2GihW1gMccJE8BxVrdnF35QsW3vJRbLRwimSYk_Oc5u2Qos8LDzA41UiJla6y6Cke6Un5fRUONVJeL/s1600/ExceptionFinal.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;467&quot; data-original-width=&quot;1066&quot; height=&quot;276&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAtx91hEKGq59Vx9IpoEhNms5KaL_fromYTjqMUWHAXXhvFjM2_ARU4t4WcyyCNT2GihW1gMccJE8BxVrdnF35QsW3vJRbLRwimSYk_Oc5u2Qos8LDzA41UiJla6y6Cke6Un5fRUONVJeL/s640/ExceptionFinal.PNG&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Checked vs Unchecked Exceptions&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
The exceptions which are checked by the compiler&amp;nbsp; are called checked exceptions&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
FileNotFoundException&lt;br /&gt;
IOException&lt;br /&gt;
InterruptedException&lt;br /&gt;
The exceptions which are not checked by the compiler are called unchecked exception.&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
NullPointerException&lt;br /&gt;
ArrayIndexOutOfBoundException&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Note 1:&lt;/b&gt; RuntimeException and its child classed, Error and its child classes are unchecked and all the remaining are checked exceptions&lt;br /&gt;
&lt;b&gt;Note 2:&lt;/b&gt; Whether the exception is checked or unchecked compulsory it should occur at runtime only. There is no chance of occurring any exception at compile time.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;u&gt;Keywords related to exception handling:&lt;/u&gt;&lt;/b&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;try:&lt;/b&gt; Put all risky code inside the try block.&lt;br /&gt;
&lt;b&gt;catch:&lt;/b&gt; Handle exception inside the catch block.&lt;br /&gt;
&lt;b&gt;finally:&lt;/b&gt; Put resource clean up code inside finally block&lt;br /&gt;
&lt;b&gt;throw:&lt;/b&gt; To handover our created exception object to the JVM manually.&lt;br /&gt;
&lt;b&gt;throws:&lt;/b&gt; To delegate the responsibility of exception handling to the caller method.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Default exception handling in java&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
1. If an exception is raised inside any method then the method is responsible to create an Exception object with the following information.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Name of the exception&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Description of the exception&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Location of the exception&lt;br /&gt;
2. After creating the exception object the method handovers the object to the JVM.&lt;br /&gt;
3.JVM checks whether the method contains any exception handling code or not. If the method won&#39;t contain any exception handling code then JVM terminates that method abnormally and remove the corresponding entry from the stack.&lt;br /&gt;
4.JVM identifies the caller method and checks whether the caller method contains any handling code or not. If the caller method also not contain any exception handling code then JVM terminates that caller also abnormally and removes the corresponding entry from the stack. This process will continue until the main() method. If the main() method also doesn&#39;t contain any exception handling code then JVM terminates main() method and removes the corresponding entry from the stack.&lt;br /&gt;
5. Then JVM handovers the responsibility to exception handling to the default exception handler.&lt;br /&gt;
6.Default exception handler print exception information to the console in the following formats and terminates the program abnormally.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Name of the exception: description&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Location of exception(stack trace)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;The possible combination of try, catch and finally&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
import java.io.FileNotFoundException;&lt;br /&gt;
public class PossibleCombination {&lt;br /&gt;
public void method() {&lt;br /&gt;
try {}catch(NullPointerException e) {}&lt;br /&gt;
try {}catch(RuntimeException e) {}&lt;br /&gt;
try {}catch(Error e) {}&lt;br /&gt;
try {}catch(Exception e) {}&lt;br /&gt;
try {}catch(Throwable e) {}&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;try {}catch(FileNotFoundException e) {}&lt;/span&gt;//CE For checked exception it must be raised in the try block then only you can handle it.&lt;br /&gt;
try {}catch(RuntimeException e) {}catch(Exception e) {}&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;try {}catch(NullPointerException e) {}catch(NullPointerException e) {}&lt;/span&gt;//CE you can&#39;t handle same exception two times.&lt;br /&gt;
try{}catch(Exception e) {}finally{}&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;try{}finally{}catch(Exception e) {}&lt;/span&gt;//CE in catch, finally series finally block should be the last block.&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;try {}catch(RuntimeException e) {}catch(NullPointerException e) {}&lt;/span&gt;//CE if there is a relation between the exceptions then first child exception should be handled first then parent.&lt;br /&gt;
try {}catch(NullPointerException e) {}catch(RuntimeException e) {}&lt;br /&gt;
try {}finally {}&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;try {}finally {}finally {}&lt;/span&gt;//CE atmost one finally block is&amp;nbsp; allowed.&amp;nbsp;&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Multi-catch block&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
In a multi-catch block, you cannot combine catch handlers for two exceptions that share a parent-child relationship. You can only combine catch handlers for exceptions that don&#39;t share the parent-child relationship between them.&lt;br /&gt;
&lt;b&gt;For example:&lt;/b&gt;&lt;br /&gt;
try {&lt;br /&gt;
throw new FileNotFoundException();&lt;br /&gt;
}catch(FileNotFoundException |IOException ex) {&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Try with Resources&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Until 1.6v, it is a programmer nagging to close all open resources(BufferReader, Connection) explicitly.&lt;br /&gt;
What&#39;s happen, If the programmer will forget to close the resources, It causes a memory leak and therefore effects the performance of the application.&lt;br /&gt;
To overcome this problem Java people introduce try-with resource&lt;br /&gt;
concept in 1.7v which closes all the open resources automatically. But only those resources which are the child of AutoCloseable or Closeable interface.&lt;br /&gt;
All resoure reference variables should be final or effectively final, therfore we can&#39;t do reassignment within the try block like below&lt;br /&gt;
try(BufferReader br = new BufferReader(new FileReader(&quot;test.txt&quot;))){&lt;br /&gt;
br = new BufferReader(new FileReader(&quot;test.txt&quot;));// invalid, CE&lt;br /&gt;
}&lt;br /&gt;
Note: We can declare any number of resources but all these resources must be separeted with &quot;&lt;b&gt;;&lt;/b&gt;&quot; and finally block is not required . Like below&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; try(Resource1;Resource2;Resource3){}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Try-with Resources enhancement&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
Until 1.8v, the resource reference variables which are created outside of the try block can&#39;t be used directly in try-with-resources like below&lt;br /&gt;
BufferReader br = new BufferReader(new FileReader(&quot;test.txt&quot;))&lt;br /&gt;
try(br){&lt;br /&gt;
// invalid until 1.8v, Resource reference variables must be local to try block&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
But valid in Java 1.8v onwards&lt;br /&gt;
try(BufferReader br1=br){&lt;br /&gt;
// valid in 1.8v or higher version&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
But in Java 1.9v onwards&lt;br /&gt;
try(br){&lt;br /&gt;
// valid in 1.9v or higher version, but make sure resources should be either final or effectively final, means we should not perform reassignment.&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Custom Exception&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
You can create a custom exception by extending Exception or RuntimeException as per your business requirement.&lt;br /&gt;
If you want to force the caller to handle it, then create your custom exception by extending Exception otherwise create custom exception using RuntimeException.&lt;br /&gt;
You can also create by extending Throwable but it is not recommended.&lt;br /&gt;
&lt;br /&gt;
Example&lt;br /&gt;
class RecordNotFoundException extends Exception{}&lt;br /&gt;
or&lt;br /&gt;
class RecordNotFoundException extends RuntimeException{}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Assertions&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
When creating application programs, you assume many things. However, often it happens that assumptions don&#39;t hold, resulting in an erroneous condition.&lt;br /&gt;
The assert statement is used to check your assumptions about the program. If assumption test gets fail then JVM will throw AssertionError&lt;br /&gt;
For Example:&lt;br /&gt;
public class AssertionExample {&lt;br /&gt;
public static void main(String[] args) {&lt;br /&gt;
int i=Integer.MIN_VALUE;&lt;br /&gt;
if(i&amp;lt;0) {&lt;br /&gt;
//If negative value, convert into positive value&lt;br /&gt;
i=-i;&lt;br /&gt;
}&lt;br /&gt;
System.out.println(&quot;the value of i is :&quot;+i);&lt;br /&gt;
assert(i&amp;gt;=0):&quot;impossible i is negative!&quot;;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;b&gt;Note:&lt;/b&gt; You have to enable assertion before executing the above program like&amp;nbsp; below&lt;br /&gt;
&lt;b&gt;java -ea AssertionExample&lt;/b&gt;&lt;br /&gt;
the value of i is :-2147483648&lt;br /&gt;
Exception in thread &quot;main&quot; java.lang.AssertionError: impossible i is negative!&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; at AssertionExample.main(AssertionExample.java:9)&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/8771159526067814039'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4242766661380116441/posts/default/8771159526067814039'/><link rel='alternate' type='text/html' href='http://www.codewithclarity.org/2019/09/exception-handling.html' title='Exception handling'/><author><name>Mahendra Kumar</name><uri>http://www.blogger.com/profile/14839558484497723715</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAtx91hEKGq59Vx9IpoEhNms5KaL_fromYTjqMUWHAXXhvFjM2_ARU4t4WcyyCNT2GihW1gMccJE8BxVrdnF35QsW3vJRbLRwimSYk_Oc5u2Qos8LDzA41UiJla6y6Cke6Un5fRUONVJeL/s72-c/ExceptionFinal.PNG" height="72" width="72"/></entry></feed>